Use Crontab on Linux server to automate VPS

Source: Internet
Author: User
Tags vps

VPS or servers often require VPS or servers to regularly back up data, regularly restart a service, or regularly execute a program. Generally, Crontab is used in Linux, in Windows, scheduled tasks are used (Windows's Gui settings are relatively simple). The following describes how to install and use Crontab on Linux VPS/server.

Crontab is a common scheduled execution tool in Unix/Linux systems. It can run specified jobs without manual intervention.

1. Installation of Crontab

1. Install Crontab under CentOS

Yum install vixie-cron crontabs // install Crontabchkconfig crond on // set to auto start service crond start //

Note: The vixie-cron software package is the main program of cron. The crontabs software package is used to install, uninstall, or list the tables used to drive the cron daemon process.

2. Install Crontab under Debian

Apt-get install cron // Debian is installed in most cases. /Etc/init. d/cron restart // restart Crontab

Ii. How to Use Crontab

1. view the crontab scheduled execution task list

crontab -l

2. Add crontab to regularly execute tasks

crontab -e

When you enter the crontab Task Command, the default editor of crontab may be different.

As shown in, the nano editor is easy to use. Simply press the crontab command format at the end of the file, press Ctrl + x to exit, and press y to save.

In the vi Editor, press the I key, press the crontab command format at the end of the file, press the ESC key, and press ENTER wq to press the Enter key.

3. crontab Task Command writing format

Format Minute Hour Dayofmonth Month Dayofweek command
Explanation Minutes Hours Date Monthly payment Weekly command
Range 0-59 0 ~ 23 1 ~ 31 1 ~ 12 0 ~ Both 7, 0, and 7 represent Sunday.

In crontab, we often use the four symbols "*" and "-/n". Well, I 'd like to draw another table to make it clearer:

Symbol Explanation
* (Asterisk) All valid values. For example, the backup command is executed at, regardless of the number of months, days, and weeks.
, (Comma) Split multiple values. For example, execute the command at 09:30 on the first, 16th, and 20th of every month.
-(Minus sign) Represents a period of time. For example, 0 9-17 *** checkmail executes the checkmail command from to every day.
/N It indicates a long interval of n. For example, */5 * check executes the check command every 5 minutes, which is the same as 0-59/5.

The following are some examples for better understanding:

Run the backup program at every day: 0 3 ***/root/backup. sh

Run the log cleanup program at 08:30 every Sunday: 30 8 ** 7/root/clear. sh

Run the test program at 5 zero o'clock every week: 0 0 ** test

Run the wenchuan program at on January 1, May 12 every year: 0 14 12 5 */root/wenchuan

Restart php-fpm from to every 15 minutes: */15 18-23 ***/etc/init. d/php-fpm

Original article:Http://www.vpser.net/manage/crontab.html

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.