Linux crontab Command

Linux 命令大全Linux Command Reference

Linux crontabIt is a command in Linux systems used to set instructions that are executed periodically.

After the operating system is installed, this task scheduling command is started by default.

crondThe command periodically checks every minute for work to be executed, and if there is any, it automatically executes it.

Note:A newly created cron job will not execute immediately; it will take at least 2 minutes. Of course, you can restart cron to execute it immediately.

Linux task scheduling work is mainly divided into the following two categories:

  • 1. System execution work:Work that the system needs to execute periodically, such as backing up system data and clearing cache.
  • 2. Personal execution work:Work that a user needs to do periodically, such as checking the mail server for new emails every 10 minutes. These tasks can be set by each user individually.

Cron Expression Online Tool:https://www.jyshare.com/front-end/9444/

Linux Cron Scheduled Tasks:https://www.example.com/w3cnote/linux-cron-task.html

Syntax

crontab [ -u user ] file

or

crontab [ -u user ] { -l | -r | -e }

Description:

crontab is used to let users execute programs at fixed times or fixed intervals. In other words, it is similar to a user's schedule.

-u user is used to set the schedule for the specified user. The prerequisite is that you must have the permission (e.g., root) to specify another user's schedule. If -u user is not used, it means setting your own schedule.

Parameter Description:

  • -e : Execute a text editor to set the schedule. The default text editor is Vi/Vim. If you want to use another text editor, please set the VISUAL environment variable to specify which text editor to use (for example, setenv VISUAL joe).
  • -r : Delete the current schedule
  • -l : List the current schedule

View the current user's crontab file:

crontab -l

Edit the current user's crontab file:

crontab -e

Delete the current user's crontab file:

crontab -r

List a specific user's crontab file (requires appropriate permissions):

crontab -u username -l

Edit a specific user's crontab file (requires appropriate permissions):

crontab -u username -e

Format

The time format is as follows:

f1 f2 f3 f4 f5 program
  • Among them, f1 represents minutes, f2 represents hours, f3 represents the day of the month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed.
  • When f1 is *, it means program is executed every minute; when f2 is *, it means the program is executed every hour; and so on for the rest.
  • When f1 is a-b, it means execution is performed from the ath minute to the bth minute; when f2 is a-b, it means execution is performed from the ath to the bth hour; and so on for the rest.
  • When f1 is */n, it means execution every n minutes interval; when f2 is */n, it means execution every n hours interval; and so on for the rest.
  • When f1 is a, b, c,..., it means execution at minutes a, b, c,...; when f2 is a, b, c,..., it means execution at hours a, b, c,...; and so on for the rest.
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

Users can also store all settings in a file first, and use crontab file to set the execution time.

Time SettingsMeaning
* * * * *Execute once every minute
0 * * * *Execute once at the 0th minute of every hour
0 0 * * *Execute once at midnight (0:00) every day
0 0 * * 0Execute once at midnight (0:00) every Sunday
0 0 1 * *Execute once at midnight (0:00) on the first day of every month
0 0 L * *Execute once at midnight (0:00) on the last day of every month
0 0 1 1 *Execute once at midnight (0:00) on the first day of every year
0 0 * * 3Execute once at midnight (0:00) every Wednesday
0 0 1,15 * *Execute once at midnight (0:00) on the 1st and 15th day of every month
0 0 * * FRIExecute once at midnight (0:00) every Friday
0 0 * * 5Execute once at midnight (0:00) every Friday
0 8-17 * * *Execute once every hour from 8 AM to 5 PM every day
0 12 * * MONExecute once at noon (12:00) every Monday
0 0 15 * *Execute once at midnight (0:00) on the 15th day of every month
0 0 * * 3Execute once at midnight (0:00) every Wednesday
0 8-17 * * *Execute once every hour from 8 AM to 5 PM every day
0 0 * * 1-5Execute once at midnight (0:00) every weekday
0 0 1 * FRIExecute once at midnight (0:00) on the first Friday of every month
0 0 1,15 * *Execute once at midnight (0:00) on the 1st and 15th day of every month
0 0 15 1 *Execute once at midnight (0:00) on January 15 every year
0 0 * * 7Execute once at midnight (0:00) every Sunday
0 0 * * 5Execute once at midnight (0:00) every Friday

Examples

Execute /bin/ls every minute:

* * * * * /bin/ls

In December, every day from 6 AM to 12 PM, execute /usr/bin/backup every 3 hours at 0 minutes:

0 6-12/3 * 12 * /usr/bin/backup

Send a letter to alex@domain.name at 5:00 PM every day from Monday to Friday:

0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata

Every day, at 0:20, 2:20, 4:20, etc., execute echo "haha":

20 0-23/2 * * * echo "haha"

Now let's look at a few more specific examples:

0 */2 * * * /sbin/service httpd restart  意思是每两个小时重启一次apache 

50 7 * * * /sbin/service sshd start  意思是每天7:50开启ssh服务 

50 22 * * * /sbin/service sshd stop  意思是每天22:50关闭ssh服务 

0 0 1,15 * * fsck /home  每月1号和15号检查/home 磁盘 

1 * * * * /home/bruce/backup  每小时的第一分执行 /home/bruce/backup这个文件 

00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;  每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。

30 6 */10 * * ls  意思是每月的1、11、21、31日是的6:30执行一次ls命令

Note:After the program runs at the time you specified, the system will send an email to the current user showing the output of the program. If you do not wish to receive such emails, please add a space after each line and then add> /dev/null 2>&1That's it, for example:

20 03 * * * . /etc/profile;/bin/sh /var/www/example/test.sh > /dev/null 2>&1 

Script Execution Issues

If we use crontab to execute a script on a schedule but it fails to execute, yet executing directly via a command (such as ./test.sh) works normally, this is mainly because the environment variables cannot be read.

Solution:

  • 1. All commands need to be written in absolute path form, e.g.:/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/crontabadd environment variables, and add the command before the executable command. /etc/profile;/bin/shso that the environment variables take effect, for example:

    20 03 * * * . /etc/profile;/bin/sh /var/www/example/test.sh

Linux 命令大全Linux Command Reference

Other Extensions