English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux crontab command

Linux Command大全

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:

  • 1, System tasks: Periodic tasks that the system needs to perform, such as backing up system data, clearing caches
  • 2, Personal tasks: Regular tasks that a user needs to perform, such as every10Check the mail server for new messages at the minute, and these tasks can be set by each user individually

Syntax

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:

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

The time format is as follows:

f1 f2 f3 f4 f5 program
  • where f1 is represents the minute, f2 represents the hour, f3 represents the day of the month, f4 represents the month, f5 represents the day of the week. program represents the program to be executed.
  • when f1 is * means to execute program every minute, f2 is * means to execute the program every hour, and so on
  • when f1 is a-b means to execute within the time period from the a minute to the b minute, f2 is a-b means to execute from the a to the b hour, and so on
  • when f1 is */n means to execute once every n minutes, f2 is */n means to execute once every n hours, and so on
  • when f1 when a, b, c,... is used, it means to execute the a, b, c,...th minute, f2 when a, b, c,... is used, it means to execute the a, b, c,...th hour, and so on
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 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.

Online example

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 

Script cannot be executed problem

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

    Linux Command大全