Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux crontab Command

Linux crontab Command

Linux Command Reference Linux Command Reference

Linux crontab is a command in Linux used to schedule commands to be executed periodically.

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

crond The command checks every minute for jobs to be executed and automatically runs them if any exist.

** Note: **A newly created cron job will not run immediately; it takes at least 2 minutes. Of course, you can restart cron to run it right away.

Linux task-scheduling jobs are mainly divided into the following two categories:

Online cron expression tools:https://www.jyshare.com/front-end/9444/

Linux Cron scheduled tasks:https://www.runoob.com/w3cnote/linux-cron-task.html

Syntax

crontab [ -u user ] file

or

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

Explanation:

crontab is used to let users run programs at fixed times or fixed intervals; in other words, it is like a user's schedule.

-u user means setting the schedule for the specified user. This requires that you have the appropriate privileges (such as root) to specify another person's schedule. If -u user is not used, it means setting your own schedule.

Parameter Description:

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 user's crontab file (requires appropriate permissions):

crontab -u username -l

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



crontab -u username -e


Format

The time format is as follows:

f1 f2 f3 f4 f5 program



*    *    *    *    *

-    -    -    -    -

|    |    |    |    |

|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)

|    |    |    +---------- 月份 (1 - 12) 

|    |    +--------------- 一个月中的第几天 (1 - 31)

|    +-------------------- 小时 (0 - 23)

+------------------------- 分钟 (0 - 59)

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

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

Example

Run /bin/ls once every minute:

* * * * * /bin/ls

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

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

At 0:20, 2:20, 4:20... every day of the month, run echo "haha":

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

Let's look at a few more concrete 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: **When the program runs at the time you specified, the system sends an email to the current user showing the program's output. If you do not wish to receive such mail, add > /dev/null 2>&1 after a space at the end of each line, for example:

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

Script execution failure

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

Solutions:

. /etc/profile . ~/.bash_profile

Linux Command Reference Linux Command Reference