当前位置:网站首页>Usage and description of crontab

Usage and description of crontab

2020-11-10 15:18:45 xxjbs001

adopt crontab command , We can execute specified system instructions or shell script Script . The time interval can be in minutes 、 Hours 、 Japan 、 month 、 Any combination of weeks .

Here's a look at crontab The format of :

# 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

crontab Get ready :

 

crontab In this experimental environment, we need to make some special preparations , First we will start rsyslog, So that we can use the information in the log to know whether our task is actually performed ( In this experiment environment, it is necessary to start manually , And in my own local Ubuntu Will default to self start, do not need to manually start )

sudo apt-get install -y rsyslog
sudo service rsyslog start

service-rsyslog-start

In this experimental environment crontab And it is not started by default , At the same time, it can not be in the background by upstart To manage , So we need us to start it ( Also in this experimental environment, you need to manually start , My own local Ubuntu And there is no need to start manually in the environment )

sudo cron -f &

 Here, enter a description of the picture

crontab Use :

 

Next, it will start crontab Used , We add a scheduled task with the following command

crontab -e

This is the first time you start it , This is the tool that lets us choose to edit , Choose the second basic vim That's all right.

 Here, enter a description of the picture

And after choosing, we will enter such a picture , That's where you add a plan , Same as general configuration documentation , With # The first part of the sign is the comment , Through the last row of the document, we can guess crontab What is the format of ?

 Laboratory Building

The detailed format can be used from the previous section man Command view :

man crontab

After understanding the command format , We use such an example to complete the addition of a task , Add this line of commands to the last line of the document , The task is that we will be in every minute /home/shiyanlou Create a blank file with the name of the current year, month, day, hour, minute, second in the directory

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

Be careful  “ % ” stay crontab In file , There's an end command line 、 Line break 、 The role of redirection , Add in front ” \ ” Symbol escape , otherwise ,“ % ” The symbol will perform its function of closing the command line or wrapping , And the subsequent content will be sent as standard input to the previous command .

After the addition is successful, we will get the last row installing new crontab A tip for

 Laboratory Building

Of course, we can also use such an instruction to see what tasks we have added

crontab -l 

By showing , We can also see that , We correctly saved and added the task's

 Laboratory Building

Although we added tasks , But if cron The daemons for did not start , It doesn't detect a task at all , Of course, it won't help us to do it , We can go through the following 2 In a way to determine our cron Whether it is successfully started in the background , Help us do things silently , If not, you have to perform the second step in the above preparation

ps aux | grep cron

or

pgrep cron

 Here, enter a description of the picture

From this screenshot, we can see that after the task is created, a file of that time is created ( That is to say 13 spot 20 branch 15 seconds ), And then, every minute 01 We do our task in seconds

 Here, enter a description of the picture

Through such a command, we can see the information feedback in the log after executing the task command

sudo tail -f /var/log/syslog

From the diagram we can see that the difference is 13 spot 28、29、30 Points of 01 Seconds for us in shiyanlou The user created a file in the home directory

 Here, enter a description of the picture

When we don't need the task, we can use this command to delete the task

crontab -r

We can see from the figure that we can check the task list after deleting it , The system has shown that the user has no task

 Here, enter a description of the picture

 

版权声明
本文为[xxjbs001]所创,转载请带上原文链接,感谢