Linux timed tasks detailed in seconds

Source: Internet
Author: User
Tags mysql backup

The implementation of Linux timing tasks are: cron, Anacron, at, and so on, here mainly about cron services.

Noun Explanation:

Cron is the service name, Crond is a background process, and crontab is a custom-made scheduled task table.

Package Installation:

To use the Cron service, install the Vixie-cron package and the Crontabs package first, and the two packages function as follows:

The Vixie-cron package is the main program for Cron.
The Crontabs package is a program used to install, uninstall, or list tables used to drive the cron daemon.

To see if a cron package is installed: Rpm-qa|grep Vixie-cron

To see if the Crontabs package is installed:rpm-qa|grep crontabs

If not, execute the following command to install the package (package must exist)
RPM-IVH vixie-cron-4.1-54.fc5*
RPM-IVH crontabs*

If you do not have a local installation package, you can install it online if you have a network connection

Yum Install Vixie-cron
Yum Install Crontabs

To see if the Crond service is running:

Pgrep Crond


/sbin/service crond Status


Ps-elf|grep crond|grep-v "grep"

Crond Service Operation Command:

/sbin/service crond Start //Startup service
/sbin/service Crond Stop //Shut down service
/sbin/service crond Restart //Restart Service
/sbin/service Crond Reload //Reload configuration

Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:

/sbin/service Crond Start//Startup service

/sbin/service Crond stop//Shut down service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//Reload Configuration

You can also start the service automatically when the system starts:

At the end of the/etc/rc.d/rc.local script, add:

/sbin/service Crond Start

Now cron This service is already in the process, we can use this service


Under Linux timed backup MySQL as an example to illustrate the following

Write a simple MySQL backup shell script


Da= ' Date +%y%m%d%h%m%s '
Mysqldump-u ROOT-PDONGJJ--all-database>/root/mysqlbakup/$da

Save As


0 3 * * */root/

Save exit

Related Commands----------------

crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists the user's current crontab.
crontab-e[user]-Edit user's current crontab.
crontab-d[user]-Delete the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.
Format of the crontab file: M H d M D cmd.
M: Minutes (0-59).
H: Hours (0-23).
D: Day (1-31).
M: Month (1-12).
D: Day of the week (0~6,0 = Sunday)
In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers.

The time here is just to divide, there are two scenarios that can be run in seconds, as follows:

The first scenario, of course, is to write a script that runs in the background all the time, and then loop through sleep for a while.

While True;d o


Sleep XX//interval of seconds


The second option is to use crontab.

We all know that the granularity of crontab is up to the minute, but we can still run it in a few seconds by doing it in a flexible way.

The following methods are executed every 20 seconds

* * * * * */bin/date
* * * * * * sleep 20; /bin/date
* * * * * * sleep 40; /bin/date

Description: Need to change/bin/date to your command

This approach to deal with the dozens of-second scheduled task is OK, if run every 1 seconds to add 60 records ... If you run it every second or plan one.

After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.

View crontab execution log, can be viewed in/var/log/cron*, or 0 3 * * * */root/ >/var/log/mysqlbak.log 2>&1 The log is directed out to view.

Linux timed tasks detailed in seconds

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: 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.