Cron Scheduled task command for Linux

Source: Internet
Author: User

To create a cron service for the current user

1. Type CRONTAB-E edit crontab service file

For example, the contents of the file are:

*/2 * * * */bin/sh/home/admin/jiaobeny/deletefile.sh

Save the file and exit

*/2 * * * */bin/sh/home/admin/jiaobeny/deletefile.sh

*/2 * * * * This field allows you to set when to execute the script

/bin/sh/home/admin/jiaobeny/deletefile.sh This field can be set

The script that you want to execute, note here that bin/sh refers to the command that runs the script

The path that the script holds when the face is a paragraph



2. See if the Crontab service under this user was created successfully with the CRONTAB-L command



3. Start the crontab service

General start service with/sbinrvice Crond start if the root user's cron suit

Can be crond start with sudo service, it's important to note the different versions

Linux system-initiated service commands are also different, like my virtual machine just with sudo

Service cron Restart, if you type service cron directly under root

Start to launch the service



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

5. Crontab command

The Cron service provides the crontab command to set the Cron service, and the following is the command

Some parameters and descriptions:

Crontab-u//Set a cron service for a user, the root user is typically executing

This parameter is required for 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
Crond resident command for task scheduling
Crond is a command that Linux uses to execute programs on a regular basis. When the installation completes the operating system

This is then started by default.

The Task Dispatch command. The Crond command periodically checks for any work to be performed.

, if there is a work to be done

The work will be performed automatically.



6. crontab Command options:

-u Specifies a user

-l lists a user's task schedule

-R Delete a user's task

-e Edit a user's tasks

7. cron file Syntax:

Hour of the week order

0-59 0-23 1-31 1-12 0-6 command (value fan)

0 for a task on Sunday One)

Remember the meanings of several special symbols:

"*" represents a number in the range of values,
"/" stands for "every",
"-" represents a number to a number,
"," separate a few discrete numbers
8. Writing the Task Scheduler settings file
The CRONTAB-E command can be used to edit and edit the corresponding user's Ar/spool/cron

Cron file, or you can modify/etc/crontab file directly
The specific format is as follows:
Minute Hour Day Month Dayofweek command
Minutes hours days months days per week order
Each field represents the following meanings:
Minute the first few minutes of every hour to perform the task
Hour the first few hours of the day to perform this task
Day of the month to perform the task
Month months of the year to perform this task
DayOfWeek the day of the week to perform the task
Command Specifies the program to execute
In these fields, except that "Command" is a field that must be specified each time, its

It is an optional field

field, depending on your needs. For fields that are not specified, use "*" to fill their position


Examples are as follows:
5 * * * * ls designation

The LS command is executed 5 minutes per hour
5 * * * ls specifies each

Day of the 5:30 execute LS command
7 8 * * ls specified per month

7:30 minutes of 8th, execute LS command.
5 8 6 * ls designation per year

June 8 5:30 execute the LS command
6 * * 0 ls specifies each

6:30 of Sunday executes the LS command [note: 0 for Sunday, 1 for Week 1,

And so on, can also be expressed in English, Sun said Sunday, Mon said Monday and so on

。 ]

3 10,20 * * ls 3 of 10th and 20th per month:

30 executing the LS command [note: "," used to connect multiple discontinuous periods]

8-11 * * * ls 8-11 pips per day

25-minute execution of the LS command [note: "-" to connect a continuous period of time]

*/15 * * * * ls every 15 minutes

Line once LS command [i.e. No. 0 15 30 45 60 minutes per hour to execute the LS command]

6 */10 * * ls per month, every

10 day 6:30 executes the LS command once [i.e., 1, 11, 21, 31st of the month Yes, 6:30 is executed once

ls command. ]

All executables in the/etc/cron.daily directory are executed as root every day 7:50

Thing

7 * * * Root run-parts

/etc/cron.daily [Note: The Run-parts parameter indicates that the following directory

There is an executable file. ]



9. New Scheduling task

There are two ways to add a dispatch task:
1), at the command line input: CRONTAB-E and then add the corresponding task, Wq save

Out
2), direct edit/etc/crontab file, namely Vi/etc/crontab, add phase

The task that should be.

10. View scheduling 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 execution results
Example 1: Execute the LS command 5:30 every day and output the results to the/jp/test file
5 * * * ls >/jp/test 2>&1
Note: 2>&1 indicates execution results and error messages.
Edit/etc/crontab File Configuration cron

Cron service every minute not only to read all the files within the Ar/spool/cron, but also to

Read/etc/crontab, so we can configure this file to do something with the cron service.

Cia The crontab configuration is for a user, while the edit/etc/crontab is for the system

The task. 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 as mail

This account

home=///user-run path, here 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

Execute scripts within/etc/cron.daily daily

4 * * 0 root run-parts/etc/cron.weekly

Execute scripts within/etc/cron.weekly every week

4 1 * * Root run-parts/etc/cron.monthly

Every month to execute scripts within the/etc/cron.monthly
Attention to the "run-parts" This parameter, if you remove this parameter, the back is

You can write a script name that you want to run, not the folder name.

For example:

1) in the command line input: CRONTAB-E and then add the corresponding task, Wq save the disk to exit



2) directly edit the/etc/crontab file, that is, vi/etc/crontab, add the corresponding

Task
2 * RM-RF/MNT/FB

Cron Scheduled task command for Linux

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.