Set up regular execution scripts under Linux

  
        

The following is for non-ubuntu environments, and some of the differences between ubuntu will be covered at the end of the article.

Under Linux, it is often necessary to periodically execute some scripts to implement some functions.

Under Linux, we use crontab to implement the function of regular execution script. The following describes the use of crontab. And some of the problems I encountered

I. Instructions for using crontab

1. crond is a command that linux uses to execute programs on a regular basis. This task scheduling command is initiated by default when the operating system is installed. The crond command periodically checks every minute for work to be performed, and if there is work to be performed, it will be automatically executed. The task of linux task scheduling is mainly divided into the following two categories:

a. System execution work, such as garbage cleaning, backup

b. User-defined work, such as every 5 minutes Scripts

2. crontab is a trigger for periodic tasks executed under UNIX systems. The user records the tasks to be performed periodically under this file, and then crond periodically checks the periodic execution list and executes it automatically when there is work to be performed.

The following information can be viewed by man crontab

a. /etc/crond.allow indicates the list of users allowed to use crontab

b. /etc/crond .deny indicates a list of users who are not allowed to use crontab

3. Several command formats commonly used by crontab

crontab -l //display the contents of the user's crontab file

crontab -e //Edit the contents of the user's crontab file

crontab -r //delete the user's crontab file

4. The basic format of the crontab file is as follows

* * * * * command

Time-of-day day and week orders

Each column means meaning

a. The first * indicates minutes from 1 to 59 minutes. The default * indicates that it is executed every minute, xy indicates that x ~ y is executed every minute, * /n means that it is executed every n minutes, x, y, z means that x, y, z minutes are executed. .

b. The second * indicates hour 1~23 hours, the default * indicates that it is executed every hour, xy indicates that x ~ y is executed every hour, */n means every Executed once every hour, x, y, z means x, y, z hours are executed.

c. The third * indicates the day 1~31, the default * indicates that it is executed every day, xy indicates that x ~ y is executed every day, * /n means every n days to execute Once, x, y, and z are executed in x, y, and z days.

d. The fourth * indicates the month 1 to 12, the default * indicates that it is executed every month, and the xy indicates that x to y is executed every month, */n means every Execute once every n minutes, x, y, z means x, y, z days are executed.

e. The fifth * indicates week 0 to 6 (0 means Sunday), the default is Monday to Sunday, and x-y means weekly x to week y are executed every day.

5. Some examples of crontab files

a. 30 21 * * * /usr/local/etc/rc.d/lighttpd restart //means to restart apache at 21:30 every day

b. 45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart //Represents monthly 1,10,22 4:45 restart apache

c. 0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart //Reset apache every 30 minutes between 18: 00 and 23: 00 every day

d * */1 * * * /usr/local/etc/rc.d/lighttpd restart //means restarting apache every hour

e. */5 * * * * /usr/local/etc /rc.d/lighttpd restart //Reset apache every 5 minutes

II. Specific example

1. First I create a shell script named /s in /home/chenguolin/tmp/.sh. Note that the path inside the script uses an absolute path.

The function of this script is to redirect the current date output to the out file in the current directory

2. Then set the script to execute every 2 minutes, >/dev/null 2> ;&1. The reason is because the system will send an email to the user after each execution of the task. Through this operation, the information can be redirected to /dev/null. The /dev/null file is similar to the recycle bin. The system automatically cleans up after a period of time. .

*/2 * * * * sh /home/chenguolin/tmp/s.sh >/dev/null 2>&1

Then I found n 2 minutes, The out file has no output at all.

3. After Google/Baidu has no results, I asked my colleague that the reason is because crontab may sometimes have no way to execute the two files in the user's home directory, namely .bashrc and .bash_profile

These two files have the following functions

.bashrc is the bash information for your bash shell, which is read when logging in and every time you open a new shell; Br>

The role of .bash_profile is that each user can use this file to enter shell information that is specific to their own use. When the user logs in, the file is only executed once! By default, he sets some environment variables to execute the user's .bashrc file,

so we have to manually add source /home/chenguolin/.bashrc && source /home/chenguolin/​​to the crontab file. .bash_profile

The role of source is to execute commands in the current bash environment, while scripts is to start a subshell to execute commands. In this way, if the command to set the environment variable (or alias, etc.) is written into the scripts, it will only affect the subshell and cannot change the current BASH. Therefore, when setting the environment variable through the file (command column), use the source command.

Therefore, the crontab file becomes

*/2 * * * * source && /home/chenguolin/.bashrc && source /home/chenguolin/.bash_profile & ;& sh /home/chenguolin/tmp/s.sh >/dev/null 2>&1

4. It is found that the script can be executed periodically.

If it doesn't work, you can only check it by looking up the log. Non-ubuntu Linux crond log files are saved under /var/log

III. Using crontab to execute scripts under ubuntu

Note the following questions to

1. The crontab service program under ubuntu is cron, and the default cron service log is not available, we must manually open

a. sudo vim /etc/rsyslog.d/50-default.conf

b. Locate the cron.* line and remove the comment

c. Then restart the cron service sudo service cron restart

d. This will find the cron log in /var/log The file, we can find the problem by looking at the log file

2. Under ubuntu, there is no .bash_profile file in the user's home directory, and the .bashrc file will be executed automatically, just write the following

*/2 * * * * sh /home/chenguolin/tmp/s.sh >/dev/null 2>&1

Copyright © Windows knowledge All Rights Reserved