Linux crontab timed task run shell script (Shell execution SQL file)

Source: Internet
Author: User

Do a Linux timed task today (summarize the table every 12 o'clock in the evening).

By the way, write a blog record ~ ~

Why use Linux timed tasks instead of scheduling tasks on the project? The reason is to prevent the project from collapsing for various reasons. So I used a more stable Linux timing task (I think so (^_^)).

At first I felt very simple, thought that at most half an hour to take care of,, the result from 10 points to get 12:30 only to fix (mainly my English too slag and database error caused) ...

^ ( ̄︶ ̄) ^ nonsense to this end---------------

crontab format * * * * * XXX

Time-of-day month-week order

The symbol "*" represents a number in the range of values,
"/" stands for "every",
"-" represents a number to a number,
"," separate a few discrete numbers

Example

3 10,20 * * ls 3:30 for each month 10th and 20th executes the LS command [note: "," used to connect multiple discontinuous periods]

8-11 * * * ls command is executed at 25 minutes per day at 8-11 Pips [Note: "-" used to connect a continuous period of time]

*/15 * * * * ls is executed every 15 minutes with the LS command [i.e. No. 0 15 30 45 60 minutes per hour to execute the LS command]

6 */10 * ls command is executed every 10 days each month (1, 11, 21, 31st). 6:30 executes the LS command once. ]

Now to introduce my specific steps (including writing a shell script and executing the SQL file written)

Open Linux terminal > enter the cat (I created myself) directory > Create Crontab folder and go to > Execute command crontab-e Enter edit state crontab Expression 1 * * * */CAT/CRONTAB/SBIN.SH ( Execute total.sh Script 1 o'clock in the morning every day) >

Ctrl+x Build Exit >yes (Save) > Enter > Crontab-l (if you can see the crontab you just saved, you have succeeded ~ ~)

(。。。 The reason for wasting time is that my save and quit are not the same as those written on the blog. ) Look down????

Example:

Assuming the current user is root, to establish the root user's scheduled task

Crontab-e

Select Editor, edit timed task (this assumes the editor is VI)

Press I to enter edit mode

1 * * * */cat/crontab/sbin.sh

Press ESC to exit edit mode into normal mode, enter: X or: Wq Save exit

Last few pictures ~ ~

A simple shell script that executes SQL

PrecautionsThe newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, it will be executed immediately. The following is attached to an introduction:

************************************************************************************

Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways: /sbin/service crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload configuration

You can also start the service automatically when the system starts:

at the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond start

Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:

1. Edit directly with crontab command

The cron Service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:

crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command

crontab-l//list details of a user cron service

crontab-r//delete a cron service with no users

crontab-e//Edit a user's cron service

For example, root to view your cron settings: crontab-u root-l

again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r

when editing the cron service, the edited content has some formatting and conventions, input: Crontab-u root-e

Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt

The first part of this format is the time setting, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:

minutes (0-59) hours (0-23) Date (1-31) Month (1-12) Week (0-6)//0 representative Sunday

In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:

every morning at 6.

-----------------

0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.

every two hours -----------------

0 */2 * * echo "has a break now." >>/tmp/test.txt

every two hours between 11 o'clock and 8 in the morning, eight in the morning. ----------------- 0 23-7/28 * * * echo "a good Dream:)" >>/tmp/test.txt every month, number 4th and Monday to Sunday, three a.m., 11 . ----------------- 0 4 * 1-3 command Line January 1 morning, 4. ----------------- 0 4 1 1 * Command Line After editing a user's cron settings each time, Cron automatically generates a file with the same name as this user under/var/spool/cron, the user's cron information is recorded in this file, this file can not be directly edited, can only be used CRONTAB-E To edit. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.

2. Edit the/etc/crontab file configuration cron cron Service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=/ //user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * root run-parts/etc/cron.daily //daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly

attention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.

************************************************************************************

Linux crontab timed task run shell script (Shell execution SQL file)

Related Article

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.