About regular tasks in Linux

  

cron is a timed execution tool under Linux that can run jobs without human intervention. Since Cron is a built-in service for Linux, but it does not automatically get up, 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 the service /sbin /service crond reload //reload the configuration

Of course, the runtime does not specify /sbin /can also. You can also start this service automatically at system startup: at the end of the /etc/rc.d/rc.local script: /sbin/service crond start

or service crond start

Cron this service is now in the process, we can use this service, Cron service provides the following interfaces for everyone to use

About the two ways:

If the script file runs with permission restrictions, use

method one:

directly use crontab command to edit cron service to provide crontab command to set cron service, the following is this Some parameters and instructions of the command: crontab -u //set a user's cron service, the general root user needs to execute this command crontab -l //list the details of a user cron service crontab - r //Delete a user's cron service crontab -e //Edit a user's cron service, such as root view their own cron settings: crontab -u root -l

Then, for example, root wants to delete fred Cron settings: crontab -u fred -r

Edited when editing cron service Some content formats and conventions, enter: crontab -u root -e

can also be written as crontab -e, the current user can edit the script automatically performed. The above command is generally used when the root user specifies it for other users.

Next, write an instruction:

//Execute every other minute

*/1 * * * *path/script file name

if Too many commands can be written in the form of a text file and then called. As follows: enter the vi edit mode, the edited content must conform to the following format: */1 * * * * ls >> /tmp/ls.txt

The first part of this format is for time Set, 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. Remember to write the full path of the command when calling. . 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) Weeks (0-6) //0 represents Sunday

In addition to the numbers, there are several 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: 6:06 every morning * * * echo "Good morning." >> /tmp/test.txt //Note that simple echo does not see any output from the screen. Because cron emails any output to the root mailbox. Every two hours 0 */2 * * * echo "Have a break now." >> /tmp/test.txt every two hours between 11pm and 8am, 8:00 AM -7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt Every month on the 4th and every week from Monday to Wednesday at 11am 0 11 4 * 1-3 command line January 1st at 4 am 0 4 1 1 * command line After editing a user's cron settings, cron automatically generates a user with the same name in /var/spool/cron The file, the user's cron information is recorded in this file, this file can not be directly edited, 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.

Addition: When using crontab, it is important to note that the environment variables that can be accessed in the running script are not consistent with the environment variables in the current test environment. A safer approach is to run the script. Set the environment variable (export) by yourself

(1) First create a file crond.txt as follows, restart at 5:36 every morning

36 5 * * * reboot

(2) upload to /opt directory

(3) run the command

crontab /opt/crond.txt

crontab -l

let The configuration file takes effect: If you want the configuration file to take effect, you have to restart cron, remember, since the cron configuration file under each user is modified. Also restart the cron server.

In Fedora and Redhat, we should use it;

[root@localhost ~]# /etc/init.d/crond restart

If you let crond boot up Run, should change its run level;

[root@localhost ~]# chkconfig --levels 35 crond on

service crond status View cron service status, if not started, service crond start starts It, the cron service is a timed execution service, you can add or edit tasks that need to be executed regularly through the crontab command.

After each execution, the system will send an email notification to the user /var/spool/mail/<USERNAME>

Note that the call script time recorded in the email notification is the time at which the script execution ends, or there is a certain delay. If you don't want to receive such a message, add > /dev/null 2>&1 after each line.

If you do not have permission restrictions you can specify it as a timed running script for the system, use

Method 2:

Edit /etc/crontab file configuration cron< Br>

The cron service not only reads all the files in /var/spool/cron every minute, but also needs to read /etc/crontab once, so we can use the cron service to do some things. The configuration with crontab is for a certain 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 scripts 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 /etc/cron every week. Script within weekly

Copyright © Windows knowledge All Rights Reserved