linux: task schedule crontab

Introduction to crontab

The crontab command is common in Unix and Unix-like operating systems (Linux is a Unix-like operating system), and is used to set instructions that are executed periodically.

The crontab command reads instructions from input devices and places them in a crontab file for later reading and execution. Usually, the instructions stored in crontab are activated by the daemon process, crond is its daemon process, and crond often runs in the background, checking every minute whether there are scheduled jobs to be executed.

Through the crontab command, we can execute specified system instructions or shell script scripts at regular intervals. The unit of time interval can be any combination of minutes, hours, days, months, and weeks.

crontab format:

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

 

Use of crontab

1. Start the log server

$ sudo service rsyslog start

In the experimental environment of the laboratory building, you need to start manually, while in your own local Ubuntu will start by default and do not need to start manually.

 

2. Start the crontab service

$ sudo cron -f &

In the experimental environment of the laboratory building, you need to start manually, while in your own local Ubuntu will start by default and do not need to start manually.

 

3. Enter the crontab file

$ crontab -e

 

 4. You can learn about the crontab command format through the man command

$man crontab

 

5. After editing vim
, we use such an example to complete the addition of a task. Add such a row of commands to the last row of the document. The task is that every minute we will create a directory with the current name in the /home/shiyanlou directory. A blank file with the name of the year, month, day, hour, minute and second

*/1 * * * * touch /home/shiyanlou/$(date +\%Y\%m\%d\%H\%M\%S)

Note that “ % ” in the crontab file has the functions of ending the command line, newline, and redirection, and the “\” symbol is added in front of it to escape, otherwise, the “%” symbol will perform its function of ending the command line or newline, and after that The contents of the command will be sent as standard input to the preceding command.

After the addition is successful, we will get a prompt for the last row of installing new crontab

 6. See what tasks we've added

$crontab -l

7. Determine whether our cron is successfully started in the background by the following 2 methods

ps to | grep cron

#or

pgrep cron

8. View the information feedback in the log after executing the task command

sudo tail -f /var/log/syslog

9. Delete the task when the task is not needed

$crontab -r

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348353&siteId=291194637