Automatic Execution of script files in Linux

Source: Internet
Author: User

Automatic Execution of script files in Linux

Since the recent project involves the automatic running of Linux shell scripts and script files, I plan to conquer it by writing a blog. Let's learn together:


This article also adopted some excellent blogs on the internet, coupled with the actual operations on the virtual machine.


The tool for automatically executing script files in Linux is Cron, which enables a script or program to run automatically without human intervention. cron is a built-in service in Linux, but it does not automatically get up. We need to use the command to enable or disable it:
Commands include:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

You can also directly use/service crond XXX without adding the previous/sbin.


Cron can also run automatically when the system is started. Add the following at the end of the script/etc/rc. d/rc. Local:

/Sbin/service crond start or service crond start


Okay, even if the above two methods start Cron, the following will be edited using the crontab command.
First, let's talk about some parameters of the crontab command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // delete a user's cron Service

Crontab-E // edit a user's cron Service


You can blur these commands, for example:
For example:
1. you log on as the root user. If you want to view your cron settings, enter crontab-u root-L.

2. If you want to edit Cron, enter crontab-u root-E or: crontab-E (edit as the current user)


Well, if you execute the preceding command statement, you must enter a VI editing interface. Haha, what do you want to enter.
The content here can be divided into two parts: the first part is the time you want to execute the script or program, and the last part is the path where your script or program is located. For details, refer:
Previous part:
Minute (0-59) hour (0-23) Date (1-31) month (1-12) Week (0-6) // 0 indicates Sunday, note that the numbers must be separated by spaces. Do not write them in sequence. In addition, there are several special symbols, including "*", "/", "-", and "-",", ", * indicates all numbers in the value range,"/"indicates each meaning, and" */5 "indicates every five units, "-" represents a number from a number to a number, "," separate several discrete numbers. The rest is to combine them according to your own needs.
Later part:
This part is simple, that is, your file path and file name.

For example, if you want to execute/root/Hello. Sh at nine o'clock A.M. every day, you only need to input:

0 9 ***/root/Hello. Sh

Save and exit. After a user's cron settings are edited, cron automatically generates a file with the same name as this user under/var/spool/cron, the cron information of this user is recorded in this file. This file cannot be edited directly. You can only use crontab.
-E to edit (also available in VIM: # Vim
/Var/spool/cron/root ). The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified. Well, this shell file will be executed on time at every morning.


The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:


Shell =/bin/bash

Path =/sbin:/bin:/usr/sbin:/usr/bin

Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.

Home = // path of the user running. The root directory is used here.

# 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 // run 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 // run the script in/etc/cron. Monthly every month.

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to run, instead of a folder name.

/Usr/lib/cron. allow indicates who can use the crontab command. If it is an empty file, it indicates that no user can schedule
Industry. If this file does not exist and another file/usr/lib/cron. deny exists, only
Users can use the crontab command. If it is an empty file, it indicates that any user can arrange jobs. When two files exist simultaneously
Cron. allow takes precedence. If none of them exist, only super users can arrange jobs.


Is it simple enough?
Post this article first. I am still studying it. It will be updated in the future. It will surely be done. cron has conquered me!

Leave a message if you have any questions. ^_^

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.