Ubuntu cron scheduled task execution

Source: Internet
Author: User

From: http://blog.chinaunix.net/u3/111961/showart_2359262.html

 

Cron is a Linux scheduled execution tool that can run jobs without human intervention.

1. About crontab

In ubuntu server 9.10, cron is installed and started by default. The/etc/crontab file shows the following content:
Bytes -----------------------------------------------------------------------------------------------------------------------
#/Etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the 'crontab'
# Command to install the new version when you edit this file
# And files in/etc/cron. d. These files also have username fields,
# That none of the other crontabs do.

Shell =/bin/sh
Path =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# M h Dom mon Dow USER command
17 * root CD/& run-parts -- Report/etc/cron. Hourly
25 6 * root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. daily)
47 6 ** 7 root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. Weekly)
52 6 1 ** root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. Monthly)
#
Bytes -----------------------------------------------------------------------------------------------------------------------
Ununtu regularly runs all the scripts in four directories by calling the Run-parts command.
1)/etc/cron. Hourly, the scripts in the directory will be executed once every hour and run at 17 minutes every hour;
2)/etc/cron. daily, the scripts in the directory will be executed once a day and run at 06:25 every day;
3)/etc/cron. weekly. The scripts in the directory are executed once a week and run at 06:47 on the seventh day of each week;
4)/etc/cron. mouthly. The scripts in the directory are executed once a month and run at 06:52 on the first day of each month;
Of course, the above time is the default system time and can be modified as needed.

2. Start and Stop the cron Service

In ubuntu 9.10, cron is installed and started by default. In ubuntu, cron is started, stopped, and restarted by calling the script in/etc/init. d. The command is as follows:
1)/sbin/service crond start // start the service
2)/sbin/service crond stop // close the service
3)/sbin/service crond restart // restart the service
4)/sbin/service crond reload // reload the configuration
You can run the following command to check whether cron is running (if it is running, a process ID is returned ):
# Pgrep Cron

3. crontab

The crontab command is used to install, delete, or list tables used to drive cron background processes. That is to say, the user puts the command sequence to be executed into the crontab file for execution. Each user can have its own crontab file. The following are some parameters and descriptions of this command:
1) crontab-u // set a user's cron Service
2) crontab-l // list the details of a user's cron Service
3) crontab-r // Delete the cron service of no user
4) crontab-E // edit a user's cron Service
The syntax of the/etc/crontab file is as follows:
Minute hour day month dayofweek command
Minute hour day week command

The meaning and value range of each field are as follows:
Minute: minute (0-59), indicates the number of minutes of each hour to execute this task
Hour: hour (1-23), which indicates the hour of the day to execute the task.
Day: the date (1-31), which indicates the day of each month to execute this task.
Month: Month (1-12), indicating the number of months of the year to execute this task
Dayofweek: the day of the week (0-6, 0 indicates Sunday ).
Command: Specifies the command to be executed (if too many commands are to be executed, you can write these commands into a script and then directly call this script here, when calling the command, remember to write the complete path of the command)
In these fields, except that "command" is a field that must be specified each time, other fields are optional fields, which can be determined as needed. For unspecified fields, use "*" to fill their positions. At the same time, cron supports writing similar to regular expressions and supports the following special symbol definitions:
"*" Indicates all numbers in the value range;
"/" Indicates "every" ("*/5", indicating every 5 units );
"-" Indicates a number ("1-4", indicating 1-4 units );
",", Separate several discrete numbers;
Example:
* Ls // specifies that the LS command is executed once every 5th minutes of an hour.
30 5 *** ls // specify to execute the LS command at every day
30 7 8 ** ls // specify to execute the LS command at on the 8 th of every month
50 7 * root run-parts/etc/cron. daily // execute all executable files in the/etc/cron. daily directory as root at every day

4. added a Cron task.


Crontab is recommended.
-E command to add a custom task (The Cron file of the corresponding user in/var/spool/cron is edited, and the crontab file in/var/spool/cron is
You cannot directly create or modify the crontab file. The crontab file is obtained through the crontab command ).
 
# Crontab-e

1) directly execute the command line
Print a string "Hello World" every 2 minutes and save it to the/home/laigw/cron/helloworld.txt file. The Cron format is as follows:
*/2 ***** echo "Hello world.">/home/helloworld.txt

2) Shell File
Call the/home/laigw/cron/test. Sh file every 3 minutes. The Cron format is as follows:
*/3 */home/laigw/cron/test. Sh
The file/home/laigw/cron/test. Sh contains the following content:

Bytes -----------------------------------------------------------------------------------------------------------------------
#! /Bin/sh
CD/home/laigw/cron
Echo "shell"> shell.txt

Bytes -----------------------------------------------------------------------------------------------------------------------

3) PHP files

Add the command line "#! /Usr/local/PHP/bin/PHP ", its"/usr/local/PHP/bin/PHP"
It means that/bin/PHP under the installation directory of the PHP program needs to be introduced.
File (in this Ubuntu system, the installation directory of the PHP program is/usr/local/PHP), and there are two ways to import files.
A. Introduce the command line in the PHP file (recommended). The Cron format is as follows:
*/1 *****/home/laigw/cron/test. php
The file/home/laigw/cron/test. php contains the following content:

Bytes -----------------------------------------------------------------------------------------------------------------------
#! /Usr/local/PHP/bin/PHP
<? PHP
$ File = '/home/laigw/cron/PHP/first_'.date('h'{.'-'.date(' I '{.'-'date('s'{.'.txt ';
File_put_contents ($ file, date ('Y-m-d h: I: s '));
?>

Bytes -----------------------------------------------------------------------------------------------------------------------
Note:

Execute the test. php file: # chmod + X test. php
B. Introduce the command line when writing a Cron task. The Cron format is as follows:
*/1 *****/usr/local/PHP/bin/PHP/home/laigw/cron/test. php
The file/home/laigw/cron/test. php contains the following content:

Bytes -----------------------------------------------------------------------------------------------------------------------
<? PHP
$ File = '/home/laigw/cron/PHP/second_'.date('h'{.'-'.date(' I '{.'-'date('s'{.'.txt ';
File_put_contents ($ file, date ('Y-m-d h: I: s '));
?>

Bytes -----------------------------------------------------------------------------------------------------------------------
Note:

Execute the test. php file: # chmod + X test. php

5. Others



/Var/spool/cron/This directory stores the cron services of all users
/Var/log/cron records cron running logs

6. crontab of a Super User

# Run the 'atrun' program every minutes
# This runs anything that's due to run from 'at'. See man 'at' or 'atrun '.
, 20, 25, 30, 35, 40, 45, 50, 55 ***/usr/lib/Atrun
40 7 * updatedb
, */Bin/Sync

7. Example

In this way, the system is configured to automatically restart at every morning.

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.