Linux settings execute scripts on a regular basis

Source: Internet
Author: User

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

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

 I. Description of the use of crontab

1. Crond is the command that Linux uses to execute programs on a regular basis. When the operating system is installed, the task Scheduling command is started by default. The Crond command periodically checks to see if there is work to be performed, and the work is done automatically if there is work to be done. and Linux task scheduling is mainly divided into the following two categories

A. Work performed by the system, such as garbage cleanup, backup

B. User-defined work, such as executing a script every 5 minutes

2. Crontab is a trigger for regular task execution under UNIX systems. The user records the tasks to be performed regularly under this file, then Crond periodically to check the regular execution list, and automatically executes the work to be performed.

The following information can be viewed through the man crontab

A./etc/crond.allow represents a list of users who are allowed to use crontab

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

3. Several command formats commonly used by crontab

Crontab-l//Displays the contents of the user's crontab file

CRONTAB-E//Edit the contents of the user's crontab file

Crontab-r//delete user's crontab file

4. The basic format of the crontab file is shown below

* * * * command

Time-sharing day and Week command

Each column represents meaning

A. The first * represents the minute 1-59 minutes, the default * is executed every minute, X-y is x~y this segment is executed every minute, */n is executed every n minutes, x,y,z is x,y,z minutes.

B. The second * represents an hour 1-23 hours, the default * is performed every hour, X-y is x~y this is performed every hour, */n is executed every n hours, x,y,z represents x,y,z hours.

C. The third * represents the day 1~31, the default * is executed every day, X-y is x~y this section of the daily implementation, */n said every n days to execute once, X,y,z is the x,y,z days are implemented.

D. The fourth * is the month 1~12, the default * is to be executed every month, X-y is the x~y of this paragraph every month, */n said every n minutes to execute, X,y,z is X,y,z days are implemented.

E. The fifth * represents the week 0~6 (0 for Sunday), the default Monday ~ Sunday are performed, X-y week x~ week y are executed every day.

5. Some examples of crontab documents

A. * * * */usr/local/etc/rc.d/lighttpd restart//means to restart Apache 21:30 every day

B. 4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart//means 4:45 restart Apache per month 1,10,22

C. 0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart//means to restart Apache every 30 minutes from 18:00 to 23:00 every day

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

E. */5 * * * */usr/local/etc/rc.d/lighttpd restart//means restart Apache every 5 minutes

  Two. Specific examples

1. First I create a shell-script name for s.sh in/home/chenguolin/tmp/. 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 every 2 minutes to execute this script once, >/dev/null 2>&1. The reason is because the system will send a message to the user after each execution of the task, through this operation can redirect the information to the/dev/null,/dev/null this file similar to the Recycle Bin for a period of time the system automatically clean up.

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

Then found that after N 2 minutes, the out file has no output at all.

3. After Google/Baidu no results, asked colleagues, got the reason because crontab may sometimes have no way to execute the user home directory of the following two files, respectively, is. BASHRC and Bash_profile

The functions of these two documents are as follows

The BASHRC role is used for bash information for your bash shell, which is read when you log in and each time you open a new shell;

The Bash_profile effect is that each user can use this file to enter shell information that is specific to their own use, and that file executes only once when the user logs on! By default, he sets some environment variables to execute the user's. bashrc file,

So we're going to manually add SOURCE/HOME/CHENGUOLIN/.BASHRC && source/home/chenguolin/.bash_profile to the crontab file.

The effect of source is to execute commands in the current bash environment, and scripts to start a child shell to execute the command. This way, if you write commands for setting environment variables (or alias, etc.) into scripts, you will only affect the child shell and cannot change the current bash, so you use the source command to set the environment variable through the file (command column).

So the crontab file becomes

*/2 * * * source &&/HOME/CHENGUOLIN/.BASHRC && source/home/chenguolin/.bash_profile && Sh/hom e/chenguolin/tmp/s.sh >/dev/null 2>&1

4. Discovery can be timed to execute the script.

If not, you can only find the log to check the error. Non-Ubuntu Linux crond log files are saved under/var/log

  Three. Use Crontab to execute scripts in Ubuntu

Note the next few questions to

1. Ubuntu under the Crontab service program is cron, and the default cron service log is not, we have to manually open

A. Sudo vim/etc/rsyslog.d/50-default.conf

B. Find the cron.* line and remove the notes.

C. Then restart the Cron Services sudo service cron restart

D. So that we can find a cron log file inside the/var/log, 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 it automatically executes the. bashrc file, as long as it is written as follows

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.