Linux crontab Command
Linux crontab
is a command used to schedule programs to be executed at regular intervals.
After installing the operating system, this task scheduling command is activated by default.
The crond command checks every minute if there are tasks to be executed and automatically runs them if there are.
Note: Newly created cron tasks will not execute immediately; they will start at least 2 minutes later. However, you can restart cron to execute them immediately.
Linux task scheduling is mainly divided into two types:
- System tasks: Work that the system needs to perform periodically, such as backing up system data and clearing caches.
- Personal tasks: Work that a specific user needs to perform regularly, such as checking for new mail every 10 minutes, which can be set up by each user.
Syntax
crontab [ -u user ] file
or
crontab [ -u user ] { -l | -r | -e }
Explanation:
crontab is used to schedule programs to be executed at fixed times or intervals, similar to a user's timetable.
-u user specifies the timetable for a designated user, which requires the necessary permissions (such as root) to designate another user's timetable. If -u user is not used, it indicates setting up one's own timetable.
Parameter Explanation:
- -e: Launches a text editor to set the timetable. The default text editor is VI. If you want to use another text editor, set the VISUAL environment variable to specify it (e.g., setenv VISUAL joe).
- -r: Deletes the current timetable.
- -l: Lists the current timetable.
The time format is as follows:
f1 f2 f3 f4 f5 program
- f1 represents minutes, f2 represents hours, f3 represents the day of the month, f4 represents the month, f5 represents the day of the week, and program represents the program to be executed.
- When f1 is *, it means executing program every minute; when f2 is *, it means executing program every hour, and so on.
- When f1 is a-b, it means executing from the a-th to the b-th minute; when f2 is a-b, it means executing from the a-th to the b-th hour, and so on.
- When f1 is */n, it means executing every n minutes; when f2 is */n, it means executing every n hours, and so on.
- When f1 is a, b, c,..., it means executing at the a-th, b-th, c-th minute; when f2 is a, b, c,..., it means executing at the a-th, b-th, c-th hour, and so on.
* * * * * - - - - - | | | | | | | | | +----- Day of the week (0 - 6) (Sunday is 0) | | | +---------- Month (1 - 12) | | +--------------- Day of the month (1 - 31) | +-------------------- Hour (0 - 23) +------------------------- Minute (0 - 59)
Users can also store all settings in a file and use crontab file to set the execution time.
Execution Time | Format |
---|---|
Execute every minute | * * * * * |
Execute every hour | 0 * * * * |
Execute every day | 0 0 * * * |
Execute every week | 0 0 * * 0 |
Execute every month | 0 0 1 * * |
Execute on the last day of every month | 0 0 L * * |
Execute every year | 0 0 1 1 * |
Examples
Execute /bin/ls every minute:
* * * * * /bin/ls
Within December, execute /usr/bin/backup every 3 hours from 6 AM to 12 PM:
0 6-12/3 * 12 * /usr/bin/backup
Send an email to [email protected] every day from Monday to Friday at 5 PM:
0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata
Execute echo "haha" at 0:20 AM, 2:20 AM, 4:20 AM, etc., every day:
20 0-23/2 * * * echo "haha"
Here are a few more specific examples: 0 */2 * * * /sbin/service httpd restart means restart apache every two hours.
50 7 * * * /sbin/service sshd start means start ssh service every day at 7:50.
50 22 * * * /sbin/service sshd stop means stop ssh service every day at 22:50.
0 0 1,15 * * fsck /home means check /home disk on the 1st and 15th of each month.
1 * * * * /home/bruce/backup means execute /home/bruce/backup file at the first minute of every hour.
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; means every Monday to Friday at 3:00, find and delete files named *.xxx in the /home directory that are older than 4 days.
30 6 */10 * * ls means execute ls command at 6:30 on the 1st, 11th, 21st, and 31st of each month.
Note: When a program runs at the specified time, the system sends an email to the current user with the content of the program execution. If you do not wish to receive such emails, add > /dev/null 2>&1
after a space in each line, for example:
20 03 * * * . /etc/profile;/bin/sh /var/www/tutorialpro/test.sh > /dev/null 2>&1
Script Not Executing Issue
If a script scheduled with crontab does not execute, but runs normally when executed directly (e.g., ./test.sh), this is mainly due to the inability to read environment variables.
Solution:
-
- Write all commands in absolute path form, such as:
/usr/local/bin/docker
.
-
- Use the following code at the beginning of the shell script:
#!/bin/sh . /etc/profile . ~/.bash_profile
-
- Add environment variables in /etc/crontab, and add the command
. /etc/profile;/bin/sh
before the executable command to make the environment variables take effect, for example:20 03 * * * . /etc/profile;/bin/sh /var/www/tutorialpro/test.sh