Linux cron and crontab use detailed explanation

  
 

A cron

crond is located in /etc/rc.d/init.d/crond or /etc/init.d or /etc/rc.d /rc5.d/S90crond, the most total reference /Var/lock/subsys/crond.


cron is a timed execution tool under linux (equivalent to the scheduled task under windows), which can run task tasks regularly without manual intervention. Since cron is a Linux service (deamon), you can start and shut down the service in the following way: /sbin/service crond start //Start the service /sbin/service crond stop //Turn off the service /sbin/service crond restart //Restart Service /sbin /service crond reload //reload configuration

You can also start this service automatically when the system starts: at the end of the /etc/rc.d/rc.local script : /sbin/service crond start

The cron service is now in the process, we can use this service.


Two crontab

The crontab is located in /usr/bin/crontab.


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 root The user needs this parameter crontab -l when executing this command. //List the details of a user cron service crontab -r //Delete a user's cron service crontab -e //Edit a user's cron service < Br>

For example, root to view their own cron settings: crontab -u root -l Then for example, root wants to delete fred cron settings: crontab -u fred -r When editing the cron service, the edited content has some formatting and Convention, enter: crontab -u root -e Enter vi edit mode, the edited content must conform to the following format:

*/1 * * * * ls >> /tmp/ls.txt The first part of the format is the setting of time, the latter part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, and then call this script directly here, call it. Remember to write the full path to the command. The setting of time has certain conventions. The first five * numbers represent five numbers. The range and meaning of the numbers are as follows:

minutes (0-59) hours (0-23) dates (1 -31) Month (1-12) Week (0-6) //0 for Sunday

In addition to the numbers, there are a few special symbols that are "*", "/" and " ;-",",",* represents all numbers in the range of values, "/" means each, "*/5" means every 5 units, "-" From a number to a number, "," separates several discrete numbers. Here are a few examples to illustrate the problem:

Every day at 6:00 6 * * * echo "Good morning." >> /tmp/test.txt //Note simple echo, from the screen I don't see any output, because cron emails any output to the root mailbox.

Every two hours 0 */2 * * * echo "Have a break now." >> /tmp/test.txt

11pm to 8am Every two hours, 8:00 am 23-7/2, 8 * * * echo "Have a good dream:)" >> /tmp/test.txt

each The 4th of the month and the Monday of each week to Wednesday morning 11:00 11 4 * 1-3 command line

January 1 at 4 am 0 4 1 1 * command line

After editing a user's cron settings, cron automatically generates a file with the same name as this user in /var/spool/cron. The cron information of this user is recorded in this file. This file is not Can be edited directly, can only be edited with crontab -e. After cron starts, it reads this file every time it is read, and checks if it wants to execute the commands inside. Therefore, you do not need to restart the cron service after this file is modified.


Three Edit the /etc/crontab configuration file The system-level configuration file for cron is located in /etc/crontab.

The cron service not only reads all the files in /var/spool/cron every minute, but also needs to read the /etc/crontab configuration file once, so we can use the cron service to do some things. Configuration with crontab -e is for a user, and editing /etc/crontab is a task for the system. The file format of this file is:

SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //If there is an error, or there is data output, The data is sent to this account as an email HOME=///The path the user runs, here is the root directory

# run-parts

01 * * * * root run-parts /etc/Cron.hourly //Execute the script in /etc/cron.hourly every hour 02 4 * * * root run-parts /etc/cron.daily //Execute the script in /etc/cron.daily every day 22 4 * * 0 Root run-parts /etc/cron.weekly //Execute the script in /etc/cron.weekly every week 42 4 1 * * root run-parts /etc/cron.monthly //Execute /etc/cron every month. The script inside the monthly

People pay attention to the "run-parts" parameter. If you remove this parameter, you can write the name of a script to run instead of the folder name.


Four instances

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

Copyright © Windows knowledge All Rights Reserved