Crontab is a commonly used scheduled execution tool under Unix/Linux systems, which can run specified jobs without manual intervention. Many times we have to use the Crontab command. For example, the acme.sh script mentioned in the Let’s Encrypt Wildcard free domain name SSL certificate uses the Crontab scheduled task to update the SSL certificate.
Through the crontab command, we can execute specified system instructions or shell scripts at fixed intervals. When the Linux VPS mounts Google Drive and Dropbox, we can use the Crontab command to achieve scheduled synchronization backup, and use Crontab to restart the VPS and server regularly every day. Or Nginx, PHP, MysqL services, etc.
In short, mastering the basic Crontab command syntax is very necessary for us to manage our own VPS hosts. This article explains how to use the Linux Crontab command through examples without going into in-depth exploration. It is mainly to facilitate quick query and reference in the future.
For more commands and applications related to VPS hosting, you can check out:
- Summary of Linux system monitoring commands - master CPU, memory, disk IO, etc. to find performance bottlenecks
- Three command tools Rsync, SCP, and Tar-quick solution to VPS remote website relocation and data synchronization
- Three ways to share folder directories in Linux - NFS remote mounting, GlusterFS shared storage and samba shared directories
1. Crontab view and edit restart
1. View the crontab scheduled execution task list
crontab -l
2. Edit crontab to execute tasks regularly
crontab -e
3. Delete crontab scheduled tasks
crontab -r
4. Related commands:
sudo service crond start #启动服务
sudo service crond stop #关闭服务
sudo service crond restart #重启服务
sudo service crond reload #重新载入配置
sudo service crond status #查看服务状态
2. Crontab basic format syntax
Time expression of crontab:
基本格式 :
* * * * * command
分 时 日 月 周 命令
Let’s look at an example first. Execute the backup program at 1:00 am every day: 0 1 * * * /root/wzfou.com/backup.sh
. Among them, /root/wzfou.com/backup.sh
is the script path. To use the absolute path, please see the figure below for the previous date format.
Instance 1: is executed every 1 minute
* * * * * /root/wzfou.com/backup.sh
Instance 2: Execute once every hour on the 3rd and 15th minutes
3,15 * * * * /root/wzfou.com/backup.sh
Example 3: Execute once every day from 8:00 to 11:00 on the 3rd and 15th minutes
3,15 8-11 * * * /root/wzfou.com/backup.sh
Instance 4: Executed every two days at the 3rd and 15th minutes from 8 am to 11 am
3,15 8-11 */2 * * /root/wzfou.com/backup.sh
Instance 5: Execute once every Monday at the 3rd and 15th minutes from 8 am to 11 am
3,15 8-11 * * 1 /root/wzfou.com/backup.sh
Example 6:Execute once every night at 21:30
30 21 * * * /root/wzfou.com/backup.sh
Instance 7: Execute once every month at 4:45 on the 1st, 10th, and 22nd
45 4 1,10,22 * * /root/wzfou.com/backup.sh
Example 8:Execute once every Saturday and Sunday at 1:10
10 1 * * 6,0 /root/wzfou.com/backup.sh
Instance 9: Executed every 30 minutes between 18:00 and 23:00 every day
0,30 18-23 * * * /root/wzfou.com/backup.sh
Instance 10: Executed every Saturday at 23:00 pm
0 23 * * 6 /root/wzfou.com/backup.sh
Example 11:Execute once every hour
* */1 * * * /root/wzfou.com/backup.sh
Example 12:Executed every hour between 23:00 every night and 7:00 the next day
* 23-7/1 * * * /root/wzfou.com/backup.sh
Example 13: is executed once on the first day of each week (that is, it starts at 24:00 every Sunday night).
@weekly /root/wzfou.com/backup.sh
Example 14:Execute once on the 15th of every month.
0 11 15 * * /root/wzfou.com/backup.sh
Instance 15: is executed once on the first day of each month (that is, it starts at 0 a.m. on the 1st of each month).
@monthly /root/wzfou.com/backup.sh
Instance 16: is executed once in the specified month (executed once every night at 0:00 in January, April and June).
0 0 * jan,apr,jun * /root/wzfou.com/backup.sh
Instance 17: Execute once after restarting.
@reboot /root/wzfou.com/backup.sh
Example 18:Send an email notification after the scheduled task is executed.
MAILTO="raj" 1 1 * * * /root/wzfou.com/backup.sh
Example 19: Specify shell (the default is /bin/bash)
SHELL=/bin/sh 1 1 * * * /root/wzfou.com/backup.sh
Instance 20: specifies environment variables.
PATH=/sbin:/bin:/usr/sbin:/usr/bin 1 1 * * * /root/wzfou.com/backup.sh
4. Possible problems with Crontab
4.1 Crontab does not take effect immediately
Your newly created Crontab scheduled task will need to wait for 2 minutes before it is executed after saving it. Of course, if you want to execute it immediately, you can restart Crontab. When crontab fails, you can try /etc/init.d/crond restart to solve the problem, or check the log to see if a task is executed/error is reported tail -f /var/log/cron.
4.2 Crontab does not execute
When the file path is involved in the script, write the global path. After updating the system time and time zone, cron needs to be restarted. When the manual execution of the script is OK, but the crontab does not execute, it is most likely an environment variable. You can try to directly introduce the environment variable into the crontab to solve the problem, for example:
0 * * * * . /etc/profile;/bin/sh /root/wzfou.com/backup.sh
4.3 Crontab does not have permission to execute
Pay attention to system-level task scheduling and user-level task scheduling. Only the root user and the owner of the crontab file can run -e, -l, -r and -v< Use UserName after the ept4> flag to edit, list, remove, or verify the crontab file for a specified user.
The root user's task scheduling operation can be set through "crontab -uroot -e", or the scheduled tasks can be written directly to the /etc/crontab file.
To edit another user's crontab, run the following command as root. The same format (append "-u username" to the end of the command) can also be used to list or delete crontabs.
crontab -u username -e
4.4 Notification after Crontab execution
When the Crontab scheduled task is executed at the time you specify, the system will send you a letter showing the execution content of the program, which can be seen in the log /var/log/cron. If you do not want to receive such a notification, please add > /dev/null 2>&1
after a space in each line.