Linux configuration timing task

Linux configuration timing task

There are two methods for timing tasks under Linux: the at command, and the crontab service.

at command

If we only want to run a specific task once, then we will use the at monitor program at this time.

 

Setting the at command is very simple, indicating the time to run, then when will it run. at is similar to the printing process, it will put the task in the /var/spool/at directory and run it at the specified time. The at command is equivalent to another shell. When the at time command is run, it sends commands one by one, and any command or program can be entered. The at now + time command can be used to indicate tasks.

 

Suppose you are dealing with a large database, and you need to process the data when others are not using the system, such as 3:10 am. Then we should first create the /home/kyle/do_job script to manage the database and plan to process the results in the /home/kyle/do_job file. The normal way is to launch the following command like this:

 

# at 2:05 tomorrow

 

at>/home/kyle/do_job

 

The time representation method in at> Ctrl+DAT Time

 

-----------------------------------------------------------------------

 

time example

 

-----------------------------------------------------------------------

 

Minuteat now + 5 minutes task runs in 5 minutes

 

Hour at now + 1 hour The task will run in 1 hour

 

Days at now + 3 days The task will run in 3 days

 

Weeks at now + 2 weeks The task will run in two weeks

 

Fixed at midnight task runs at midnight

 

Fixed at 10:30pm task at 10:30 pm Note: Be sure to check whether the atq service is started, some operating systems may not be started by default, linux is not started by default, and ubuntu is started by default. Check whether it is started, check the syntax with service atd, check the status of atd with service atd status, and start the atd service with service atd start.

 

Check the specific content of at execution: generally located in the /var/spool/at directory, open it with vi, and the last part is your execution program.

 

crontab

cron is a timing execution tool under linux that can run jobs without human intervention. Since Cron is a built-in service of Linux, but it does not start automatically, you can use the following methods to start and close this service:

 

/sbin/service crond start//Start service

 

/sbin/service crond stop //Close the service

 

/sbin/service crond restart //Restart the service

 

/sbin/service crond reload//Reload configuration

 

/sbin/service crond status//View service status You can also start this service automatically when the system starts:

 

At the end of the script /etc/rc.d/rc.local add:

 

/sbin/service crond start Now that the Cron service is already in the process, we can use this service. The Cron service provides the following interfaces for everyone to use:

 

Edit directly with crontab command

 

The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

 

crontab -u //Set a user's cron service, generally the root user needs this parameter when executing this command

 

crontab -l //List the details of a user's cron service

 

crontab -r //delete a user's cron service

 

crontab -e //Edit a user's cron service, such as root to view your own cron settings: crontab -u root -l

 

For another example, root wants to delete fred's cron settings: crontab -u fred -r

 

Basic format :

 

*****command

 

time-sharing sun-month-week command

 

The first column indicates minutes 1 to 59 with * or */1 for every minute

 

The second column represents hours 1 to 23 (0 means 0 o'clock)

 

The third column represents the date 1 to 31

 

The fourth column represents the month 1 to 12

 

The 5th column identifies the day of the week 0 to 6 (0 means Sunday)

 

Column 6 Command to run

Some examples of crontab files:

 

#Restart apache at 21:30 every night.

 

30 21 * * * /usr/local/etc/rc.d/lighttpd restart

 

#1, 10, 22 of every month

 

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

 

#everyday at 6:10am

 

10 6 * * * date

 

#every two hours

 

0 */2 * * * date

 

#Every two hours between 11pm and 8am, 8am

 

0 23-7/2,8 * * * date

 

#4th of every month and every Monday to Wednesday at 11am

 

0 11 4 * mon-wed date

 

#January day at 4am

0 4 1 jan * date

 

It is worth noting that in crontab, both the command and the file path must be written in full , otherwise it will not be recognized.

For example, to execute a /home/run/test.sh script at 6 o'clock every day, you can configure it as follows:

0 6 * * * /sbin/sh /home/run/test.sh 

or

0 6 * * * cd /home/run && /bin/sh test.sh

It should be noted here that the commands in the test.sh script also need to use the full path, otherwise crontab cannot find them.

 If you want to have some other requirements, such as retaining the output, you can redirect the output, which is no different from normal script running

 

0 6 * * * cd /home/run && /bin/sh test.sh > log

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943287&siteId=291194637