Create a scheduled task in liunx

Source: Internet
Author: User

Create a Cron service for the current user

1. Type crontab-e to edit the crontab service file.

For example, the file content is as follows:

*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh

Save the file and exit

*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh

*/2 ***** you can use this field to set when to execute the script.

/Bin/sh/home/admin/jiaoben/buy/deletefile. the sh field can be used to set the script to be executed. Note that bin/sh refers to the path where the script is stored when the command for running the script is followed.

 

 

2. Check whether the crontab service under the user is successfully created. Run the crontab-l command.

 

3. Start the crontab Service

Generally, the/sbin/service crond start is used to start the service. If the root user's cron service is used, the sudo service crond start can be used. Here, you should note that the commands of the services started by Linux systems of different versions are different, for example, in my virtual machine, you only need to use sudo service cron restart. If you type service cron start directly at the root, the service can be started.

 

4. Check whether the service is running with PS-ax | grep Cron

5. crontab command

The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

Crontab-u // set a user's cron service, generally, when executing this command, the root user needs this parameter crontab-l // to list the details of a user's cron service. crontab-r // Delete the cron service crontab-E of no user. // edit the cron service of a user, for example, view the cron settings of the root user: crontab-u root-l again, for example, Root wants to delete the cron setting of FRED: crontab-u Fred-r when editing the cron service, the edited content has some formats and conventions. input: crontab-u root-e enters the VI editing mode. The edited content must conform to the following format: */1 ***** ls>. After the operating system is installed

Task Scheduling command. Each worker of the crond command regularly checks whether there is any job to be executed.

The job is automatically executed.

 

6. crontab command options:

-U specifies a user

-L list the task plans of a user

-R: delete a user's task

-E. edit a user's task.

7. Cron File Syntax:

Hour, day, month, and week commands

0-59 0-23 1-31 1-12 0-6 command)

Remember the meanings of several special symbols:

"*" Indicates a number in the value range, "/" indicates "every", and "-" indicates that a number is transferred to a number. "," separates several discrete numbers.

8. you can run the crontab-e command to edit the syntax of the task scheduling setting file. The Cron file of the corresponding user in/var/spool/cron is edited, you can also directly modify the format of the/etc/crontab file as follows: minute hour day month dayofweek command minute hour day month day weekly command each field represents the meaning of the following: minute executes this task in the minute of each hour. Hour executes this task in the hour of every day. day of every month. Run this task in the month. day of every year. Run this task in the day of every week. execute this task command to specify the program to be executed in these fields, except that "command" is a field that must be specified each time, other fields are optional.

Field, depending on the needs. For unspecified fields, use "*" to fill their positions. Example: * ls indicates that the LS command is executed once every 5th minutes of every hour. * 5 * ls indicates that the LS command is executed at every day. * 7 8 * ls indicates the 8 th day of every month. execute the LS command at AM 30 5 8 6 * ls to specify to execute the LS command at AM on April 9, June 8*0 ls to specify to execute the LS command at AM on every Sunday. [Note: 0 indicates Sunday, 1 indicates Monday,

Similarly, Sun represents Sunday, mon represents Monday, and so on.]

30, 20 ** ls: Execute the LS command at on the 10th and 20th of every month [Note: "," is used to connect multiple discontinuous periods]

25 8-11 *** ls execute the LS command 25th minutes at every day [Note: "-" is used to connect consecutive periods]

*/15 * ls: Execute the LS command every 15 minutes [that is, execute the LS command every hour for 0th 15 30 45 60 minutes]

306 */10 ** ls: Execute the LS command at every 10 days. [that is, execute the LS command at on May 1, 11, 21, and 31 every month. ]

Run all executable files in the/etc/cron. daily directory as root at every day.

50 7 * root run-parts/etc/cron. daily [Note: Run-parts parameter indicates that all executable files in the following directory are executed. ]

 

9. added a scheduling task.

There are two ways to add a scheduling task: 1) Enter crontab-E in the command line and add the corresponding task. Then, the WQ disk exits. 2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task.

10. view the scheduling task crontab-l // list all the current scheduling tasks crontab-l-u JP // list all scheduling tasks of the user JP

11. Delete the task scheduling job crontab-r // delete all task scheduling jobs

12. example 1: Execute the LS command at every day, output the result to the/jp/test file for 30 5 * ls>/jp/Test 2> & 1 Note: 2> & 1 indicates the execution result and error message. Edit the/etc/crontab file configuration Cron

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 = // the path of the user running. Here is the root directory # Run-Parts

01 *** root run-parts/etc/cron. Hourly // Run hourly

Scripts in/etc/cron. Hourly

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/etc/cron every month. for the script in monthly, pay attention to the "run-parts" parameter. If this parameter is removed, you can write a script name to be run later, instead of the folder name.

For example:

1) Enter crontab-E in the command line and add the corresponding task. The WQ disk will exit.

2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task 11 2 21 10 * Rm-RF/mnt/FB

Original address: http://qa.taobao.com /? P = 9189

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.