Use Cron to set timed tasks under CentOS and Ubuntu

Source: Internet
Author: User

1. Start the Cron tool [PS: Use root privileges]

CentOS Boot cron Two ways

A)/etc/init.d/crond start

b) Service Crond start

Ubuntu Startup Cron Two ways

A)/etc/init.d/cron start

b) Service cron start (recommended)

2. Add timed tasks [execute LS command per hour]

Centos

CRONTAB-E command to open a file

Add a line: 0 * * * * ls

Ubuntu

CRONTAB-E command to open the file [PS: The first time with this command, will let you select a text editor, enter the corresponding editor number, the question is explained later]

Add a line: 0 * * * * ls

Save exit

3. View Scheduled Tasks

centos| | Ubuntu

Crontab-l

Above is the operation of a simple scheduled task,

crontab command Options

crontab file [-u user] replaces the default crontab file with the specified files, for example: Crontab/var/mycron.txt-u root

If you want to restore the default file, perform a crontab-r delete task

crontab-l [-u user] lists the current tasks for the specified user

CRONTAB-E [-u user] Edit specified user timing task

Crontab-r [-u user] Deletes the current task for the specified user

crontab file Syntax

The format is: minutes (0-59) hours (0-23) days (1-31) months (1-12) weeks (0-6) Order (command)

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

For example:

5 * * * * ls indicates the first 5 minutes of each hour to execute the LS command

5 * * 0 ls indicates the LS command is executed at 5:30 points per Sunday

3-8 * * ls indicates that the LS command is executed at the 20th point every day from 3 o'clock to 8.

*/15 * * * * * php-f/varwww/index.php means the php file is executed once per hour 0,15,30,45

    

  

Let's talk about some of the difficulties I've encountered in Ubuntu.

I forgot to choose the editor when I was crontab-e, so I came up with this interface

This is forgetting to choose Vim Editor, in fact can also be edited in the file, just a little strange, OK, continue

After adding the execution task to the last line of the file: Ctrl+x exit, then enter Y to save, then return to exit, OK, it's done.

But I'm a compulsive patient, feeling a little uncomfortable, I want to use VIM, continue

Enter sudo select-editor on the command line, select the corresponding vim serial number, OK, finally complete

Use Cron to set timed tasks under CentOS and Ubuntu

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.