English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Linux crontab is a command used to execute programs regularly.
After the operating system is installed, this task scheduling command will be started by default.
The crond command checks every minute for scheduled tasks to be executed, and if there are tasks to be executed, it will automatically execute them.
Note:Newly created cron tasks will not be executed immediately, but at least after 2 minutes before you can, of course, you can restart cron to execute immediately.
The main work of Linux task scheduling is divided into the following two categories:
crontab [ -u user ] file
or
crontab [ -u user ] { -l | -r | -e }
Description:
crontab is used to allow users to execute programs at fixed times or intervals, in other words, it is similar to the user's schedule.
-u user refers to setting the schedule for a specified user. This requires that you have the necessary permissions (such as root) to specify someone else's schedule. If not -u user means setting your own schedule.
Parameter Description:
The time format is as follows:
f1 f2 f3 f4 f5 program
* * * * * - - - - - | | | | | | | | | +----- weekday of the week (0 - 6) (Sunday is 0) | | | +---------- month (1 - 12) | | +--------------- the day of the month (1 - 31) | +-------------------- hour (0 - 23) +------------------------- minute (0 - 59)
Users can also save all settings in a file first, and set the execution time using the crontab file method.
execute once every minute /bin/ls:
* * * * * /bin/ls
in 12 within the month, every morning 6 to 12 point, every 3 execute once every hour at 0 minute /usr/bin/backup:
0 6-12/3 * 12 * /usr/bin/backup
Every afternoon from Monday to Friday every day 5:00 Send a letter to [email protected]:
0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata
Every midnight at 0 o'clock every day 20 minute, 2 dot 20 minute, 4 dot 20 minute....execute echo "haha":
20 0-23/2 * * * echo "haha"
Let's look at some specific examples below:
0 */2 * * * /sbin/service httpd restart means to restart apache every two hours 50 7 * * * /sbin/service sshd start means every day7:50 Enable ssh service 50 22 * * * /sbin/service sshd stop means every day22:50 Disable ssh service 0 0 1,15 * * fsck /home every month1number and15number check/home disk 1 * * * * /home/bruce/backup executes at the first minute of every hour /home/bruce/backup this file 00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; from Monday to Friday3o'clock, in the directory/Find the file named in home*Delete .xxx files4day. 30 6 */10 * * ls means the file before the month1、11、21、31Day yes6:30 Execute ls command once
Note:After the program executes at the time you specify, the system will send an email to the current user, showing the content of the program execution. If you do not want to receive such an email, please add '>' after a space in each line. /dev/null 2>&1 It can be done, such as:
20 03 * * * . /etc/profile;/bin/sh /var/www/w3codebox/test.sh > /dev/null 2>&1
If we use crontab to schedule the execution of the script, it cannot be executed, but if executed directly through the command (such as:./After test.sh) can be executed normally again, this is mainly because the environment variables cannot be read.
Solution:
1、All commands need to be written in absolute path format, such as: /usr/local/bin/docker。
2、Use the following code at the beginning of the shell script:
#!/bin/sh . /etc/profile . ~/.bash_profile
3、in /etc/crontab Add the environment variable in the command before the executable command, such as: /etc/profile;/bin/sh, making the environment variables effective, for example:
20 03 * * * . /etc/profile;/bin/sh /var/www/w3codebox/test.sh