Linux Timing Task Setup crontab configuration Guide __linux

Source: Internet
Author: User
Tags save file

Create a cron service for the current user

1. Type CRONTAB-E edit crontab service file

For example, the contents of the file are as follows:

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

Save File and exit

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

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

/bin/sh/home/admin/jiaoben/buy/deletefile.sh This field can set the script you want to execute, here to note that bin/sh refers to the path that the script holds after the command that runs the script

2. To see if the Crontab service under this user was created successfully, use the CRONTAB-L command

3. Start Crontab Service

General boot service with/sbin/service Crond start if the root user's cron service can use sudo services crond start, it's also different to note that the commands for different versions of the Linux system Boot service are the same, like my virtual machine requires only sudo service cron Restart can start the services if you type service cron start directly under root

4. See if the service is running with Ps-ax | grep cron

5. 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 required by the general root user when executing this command
CRONTAB-L//list details of a user's cron service
Crontab-r//Remove cron service for no users
CRONTAB-E//Edit a user's cron service
For example, root 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 a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e
into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
Crond resident command for task scheduling
Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the default is to start this

Task scheduling commands. Crond order every minute to check whether there is any work to be performed, and if there are any works to be performed

The work will be carried out automatically.

6. crontab command option:

-u Specifies a user

-l List A user's task schedule

-R Delete a user's task

-e Editing a user's task

7. cron file Syntax:

Hour and Moon Week command

0-59 0-23 1-31 1-12 0-6 Command (value range, 0 for Sunday One for a row corresponding to a task)

Remember the meaning of several special symbols:

"*" represents the number in the range of values,
"/" stands for "every",
"-" represents from a number to a number,
"," separated by several discrete figures

8. Task scheduling Setup file writing
can be edited using the Crontab-e command, edited by the/var/spool/cron of the corresponding user's cron file, or directly modify the/etc/crontab file
The specific format is as follows:
Minute Hour Day Month dayofweek command
Minutes hours days month days per week order
Each field represents the following meanings:
Minute the first few minutes of each hour
Hour a few hours a day to perform this task
Day of the month to perform this task
Month a few months of the year to perform this task
DayOfWeek to perform the task on the first day of the week
Command Specifies the program to be executed
In these fields, except for "Command", which must be specified each time, the other fields are optional

fields, depending on the need to decide. For fields that are not specified, use "*" to fill their position.
Examples are as follows:
5 * * * * ls Specifies the first 5 minutes of every hour to execute the LS command
5 * * * ls specifies 5:30 daily execution of LS command
7 8 * ls specifies 7:30 points per month 8th to execute LS command
5 8 6 * ls specifies every June 8 5:30 execution of the LS command
6 * * 0 ls Specifies 6:30 execution of the LS command per Sunday [Note: 0 means Sunday, 1 means Week 1,

By analogy, it can also be expressed in English, Sun said Sunday, Mon said Monday. ]

3 10,20 * * ls 10th and 20th 3:30 Execute ls command [note: "," used to connect multiple discontinuous periods]

8-11 * * * ls 25 minutes per day at 8-11 o ' hour to execute the LS command [note: "-" to connect consecutive periods]

*/15 * * * * ls is executed once every 15 minutes by the LS command [that is, the No. 0 15 30 45 60 minutes per hour executes the LS command]

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

Executes every executable file in the/etc/cron.daily directory as root 7:50 daily

7 * * * Root run-parts/etc/cron.daily [Note: run-parts parameter indicates that all executables in the following directory are executed. ]

9. New Scheduling tasks

There are two ways to add new scheduling tasks:
1, at the command line input: Crontab-e then add the corresponding task, Wq save the disk exit.
2, directly edit/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.

10. View Scheduled Tasks
Crontab-l//List all current Scheduled tasks
Crontab-l-u JP//List all scheduling tasks for user JP

11. Delete Task Scheduling work
Crontab-r//Delete all task scheduling work

12. Steering of task scheduling implementation results
Example 1: Execute the LS command 5:30 every day and output the result to the/jp/test file
5 * * * ls >/jp/test 2>&1
Note: 2>&1 represents execution results and error messages.
Edit/etc/crontab File Configuration cron

The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/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 there is an error, or if there is data output, the data is sent to this account as mail

home=///user Run path, this is the root directory
# Run-parts

* * * * root run-parts/etc/cron.hourly//hourly execution

Scripts within the/etc/cron.hourly

4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts

4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script

4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script
Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name

For example:

1 at the command line input: CRONTAB-E and then add the corresponding task, WQ disk exit.

2 directly edit/etc/crontab file, that is, vi/etc/crontab, add the corresponding task
2 * RM-RF/MNT/FB

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.