Latest web development tutorials

Linux crontab command

Linux command Daquan Linux command Daquan

Linux crontab is the program used to periodically execute commands.

When the installation is complete operating system, the default will start this task scheduling command.

Chung crond command checks for work to be performed on a regular basis every minute, if there is work to be performed automatically perform the job.

Working linux task scheduling is divided into the following categories:

  • 1, the work performed by the system: The system periodically work to be performed, such as data backup system, clear the cache
  • 2, the work performed by a person: a user work to be done on a regular basis, such as every 10 minutes to check whether the mail server has a new letter, which work by each user to set their own

grammar

crontab [ -u user ] file

or

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

Explanation:

crontab is used to allow users at a fixed time or at regular intervals with the enforcement proceedings, in other words, it is similar to the user's time schedule.

-u user specifies the user refers to the set time-table, the premise is that you must have their permission (for example the root) can specify the time-table of others. If you do not use the -u user, then, it is expressed to set their own time schedule.

Parameter Description:

  • -e: execute a text editor to set time-table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variable to specify the text editor (for example, setenv VISUAL joe)
  • -r: Remove the current time-table
  • -l: Lists the current time-table

Time-table has the following format:

f1 f2 f3 f4 f5 program
  • Where f1 is a minute, f2 the hour, f3 represents a month in the first few days, f4 represents the month, f5 represents a day of the week. program indicates that the program to be executed.
  • When expressed as f1 * have to perform when every minute program, f2 * indicates a program to be executed per hour, and the rest by analogy
  • A representation from the first minute to the first b minutes this time to perform, f2 have to perform when ab representation from a to b hours, the rest by analogy When f1 is ab when
  • N represents each minute time intervals once, f2 for the * / n n represents each one hour time intervals when f1 is * / n, the rest by analogy
  • Denotes a, b, c ... represents the a, b, c, ... minutes to perform, f2 for the a, b, c, ... when f1 is a, b, c, ... when hours to perform, and the rest by analogy

Users can also store all the settings in the file before using crontab file way to set time schedule.

Examples

The first monthly hour 0 minutes a day to perform a / bin / ls

0 7 * * * /bin/ls

Within 12 months, the daily 6 am to 12 pm, and every 20 minutes to perform a / usr / bin / backup

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

Monday to Friday 5:00 pm every day send a letter to [email protected]

0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata

Monthly midnight every day 0:20, 2:20, 4:20 .... execute echo "haha"

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

Next, consider a few 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: When you execute the program specified time, the system will send a letter to you to display the contents of the program execution, if you do not wish to receive such a letter, please leave a space after each line plus> / dev / null 2> & 1 to

Linux command Daquan Linux command Daquan