Linux Cron Scheduled Tasks

2022年1月21日 3214点热度 0人点赞 0条评论
内容目录

Recently, I migrated my blog system, which involved Nginx, SSL certificates, a MySQL database, and various WordPress files. After quite a bit of effort, to avoid potential issues with server failures in the future, I started a Raspberry Pi at home and used rsync to perform regular backups of the server where the blog is hosted.

The Raspberry Pi uses its built-in cron service to perform scheduled tasks.

First, open the /etc/cron.d directory and create a file that doesn't require a file extension, for example: bakblog.

The content of the file consists of three parts: cron time expression + the user running the task + the command to execute.

However, the cron time expression here is 5 fields, without seconds or weeks.
file

For example, this task runs once every hour at the 37th minute.

37 * * * * root /opt/wordpress_bak/bak.sh

You can use https://cron.qqe2.com/ to generate the expression, then delete the seconds and week fields.
For example: 0 3 * * * runs the task at 3:00 AM every day.

Then reload the cron configuration or restart the service (details to follow).

To facilitate testing and monitoring, you need to log the cron output.

sudo vim /etc/rsyslog.d/50-default.conf 

Remove the # at the beginning.

cron.*				/var/log/cron.log

Restart:

service cron reload
# or
service cron restart

Then, wait until the execution time arrives and check the logs.

root@ubuntu:/etc/cron.d# tail -f  /var/log/cron.log

Jan 21 09:35:10 ubuntu cron[150245]: (CRON) INFO (pidfile fd = 3)
Jan 21 09:35:10 ubuntu cron[150245]: (CRON) INFO (Skipping @reboot jobs -- not system startup)
Jan 21 09:36:01 ubuntu cron[150262]: (CRON) INFO (pidfile fd = 3)
Jan 21 09:36:01 ubuntu cron[150262]: (CRON) INFO (Skipping @reboot jobs -- not system startup)
Jan 21 09:37:01 ubuntu CRON[150268]: (root) CMD (/opt/wordpress_bak/bak.sh)

痴者工良

高级程序员劝退师

文章评论