Set the script for regular execution in Linux, and run the script in linux.

Source: Internet
Author: User

Set the script for regular execution in Linux, and run the script in linux.

The following section describes the differences between ubuntu and non-ubuntu environments.

In Linux, you often need to regularly execute some scripts to implement some functions.

In Linux, we use crontab to implement regular script execution. The following describes how to use crontab. And some problems I encountered


I. crontab instructions

1. crond is a linux Command Used to regularly execute programs. After the operating system is installed, the task scheduling command is started by default. Each minute, the crond command regularly checks whether there is any job to be executed. If there is any job to be executed, the job is automatically executed. Linux task scheduling mainly includes the following two types:

A. the work performed by the system, such as garbage cleaning and backup

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


2. crontab is a trigger that regularly executes tasks in UNIX systems. The user records the tasks to be periodically executed under this file, and then crond regularly checks the list of scheduled tasks. The task is automatically executed when there is a task to be executed.

Man crontab can be used to view the following information.

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

B./etc/crond. deny indicates the list of users that cannot use crontab.

3. Common command formats of crontab

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

Crontab-e // edit the content of your crontab file

Crontab-r // Delete the user's crontab file


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

* *** Command

Minute hour day month week command

Meaning of each column

A. The first * indicates the minute 1 ~ 59 minutes. By default, * Indicates execution every minute, and x-y indicates x ~ Y is executed every minute. */n indicates that x, y, and z are executed every n minutes.

B. The second * indicates the hour 1 ~ 23 hours. The default value * indicates that the task is executed every hour, and x-y indicates that the task is executed by x ~ Y is executed every hour. */n indicates that x, y, and z are executed every n hours.

C. The third * indicates the day 1 ~ 31. By default, * indicates the execution every day, and x-y indicates x ~ Y is executed every day. */n indicates that x, y, and z are executed every n days.

D. The fourth digit * indicates the month 1 ~ 12. By default, * indicates the execution every month, and x-y indicates x ~ Y is executed every month. */n indicates execution every n minutes. x, y, and z indicate that x, y, and z are executed every day.

E. The fifth * indicates the week 0 ~ 6 (0 indicates the week day). The default value is Monday ~ Executed every week, x-y indicates week x ~ Week y is executed every day.


5. Examples of crontab Files

A. 30 21 ****/usr/local/etc/rc. d/lighttpd restart // indicates that apache is restarted at every day.

B. 45 4, 22 **/usr/local/etc/rc. d/lighttpd restart // indicates that apache is restarted at on November 22, every month.

C. 18-23 ***/usr/local/etc/rc. d/lighttpd restart // indicates that apache is restarted every 30 minutes from to every day.

D. **/1 ***/usr/local/etc/rc. d/lighttpd restart // indicates that apache is restarted every hour.

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


Ii. Example

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

This script redirects the current date output to the out file in the current directory.


2. Then set to execute this script every 2 minutes,>/dev/null 2> & 1. The reason is that the system sends an email to notify the user after each task is executed. This operation can redirect all information to/dev/null, the/dev/null file is similar to the recycle bin file which is automatically cleared after a period of time.

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

Then we found that after n 2 minutes, the out file was not output at all.

3. After Google/Baidu failed, I asked my colleagues why crontab may sometimes fail to execute the two files under the user's home directory, which are. bashrc and. bash_profile.

The functions of these two files are as follows:

. Bashrc is used for bash information of Your bash shell. When you log on and open a new shell, the file is read;

. Bash_profile is used by each user to input shell information dedicated to their own use. When a user logs on, the file is executed only once! By default, it sets some environment variables to execute the user's. bashrc file,

Therefore, you must manually add source/home/chenguolin/. bashrc & source/home/chenguolin/. bash_profile to the crontab file.

Source is used to execute commands in the current bash environment, while scripts is used to start a sub-shell to execute commands. In this way, if you write the commands for setting environment variables (or alias) into scripts, it will only affect the sub-shell and cannot change the current BASH. Therefore, through the file (command column) when setting environment variables, 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. The script can be executed regularly.

If not, you can only find the error by searching for logs. Non-ubuntu Linux crond log files are stored in/var/log


Iii. Use crontab in ubuntu to regularly execute scripts

Note:The following problems are described as follows:

1. The crontab service program in ubuntu is cron, And the cron Service log does not exist by default. We must manually enable it.

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

B. Find cron. * and delete the comment.

C. restart the cron service sudo service cron restart.

D. In this way, you can find the cron log file in/var/log. Then, you can find the problem by viewing the log file.

2. In ubuntu, there is no. bash_profile file in the user's home directory, and The. bashrc file will be automatically executed. Just write it as follows:

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



What is the difference between executing a file in a linux script and automatically executing a file in a scheduled script?

If a script is called regularly, the. profile file must be forcibly executed at the beginning of the script. Otherwise, the program will not read this file and will not be able to use the environment variables in. profile.
If you do not have the. profile file in your system, you must also execute the file for setting environment variables.

Problems with running a Scheduled Update script using cron in linux

Ls-l test. sh
Check if test. sh has the executable permission. If not
Chmod + x test. sh
Or write crontab in this way.
*/1 ***** sh/home/yf/test. sh

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.