Ubuntu uses Cron to set a timed start task

Source: Internet
Author: User
Tags chmod php file ssh rsyslog
Introduction

Cron is a Linux timed execution tool that can run jobs without human intervention.
Under Ubuntu Server, Cron is installed and started by default, and if it is not started, it is set and started (chkconfig\sysv-rc-conf).
The following can be seen through its configuration file/etc/crontab:

Ununtu runs all scripts in four directories on a timed basis by invoking the Run-parts command.
1)/etc/cron.hourly, the script in the directory will be executed once per hour and run at 17 minutes per hour;
2)/etc/cron.daily, the script in the directory will be executed once a day, at 6:25 each day to run;
3)/etc/cron.weekly, the script in the directory will be executed once a week, at 6:47 on the seventh day of the week;
4)/etc/cron.mouthly, the script in the directory will be executed once a month, at the 6:52 of the month of 1th, run;
Of course, the above time is the system default time, can be modified according to their own needs. start and stop of cron service

Cron is installed and started by default. While Ubuntu starts, stops and restarts Cron, all by invoking the script in/etc/init.d/. The command is as follows

Start: Sudo/etc/init.d/cron start off: Sudo/etc/init.d/cron stop
Restart: Sudo/etc/init.d/cron Restart reload configuration: Sudo/etc/init.d/cron Reload
can be used PS aux | grep cron command to see if Cron is started

You can also use the following command

/usr/sbin/service Crond Start//Startup service
/usr/sbin/service Crond stop//Shut down service
/usr/sbin/service crond Restart//Restart service
/usr/sbin/service Crond Reload//Reload configuration to see if Cron is running

Pgrep Cron set cron run log storage path Modify Rsyslog file

Delete the # before the #cron.*;
Restart Rsyslog Service

sudo service rsyslog restart
Restart Cron Service
viewing log files
cron File Syntax

The syntax is as follows:
Minute Hour Day Month Dayofweek command
Minutes hours days months days per week order
Each field represents a range of meanings and values as follows:
Minute: Minutes (0-59), which represents the first minute of each hour to perform the task
Hour: Hour (1-23), which indicates the hours of the day to perform the task
Day: Date (1-31), which indicates the day ordinal of a month to perform the task
Month: Months (1-12), which represents the month ordinal of the year in which the task is performed
DayOfWeek: Week (0-6, 0 for Sunday), which indicates the day of the week to perform the task
Command: Specify the commands to execute (if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can remember to write the full path of the command)
In these fields, except that "Command" is the field that must be specified each time, the other fields are optional fields, which can be determined visually. For a field that is not specified, "*" is used to fill its position. At the same time, Cron supports writing similar to regular expressions, and supports several special symbol definitions as follows:
"*", representing all numbers within the range of values;
"/", on behalf of "each" ("*/5", representing every 5 units);
"–", representing a number to a number ("1-4", representing 1-4 units);
",", separating several discrete numbers;
Value range of paragraph meaning
The first paragraph represents the minute 0-59
The second paragraph represents the hour 0-23
The third paragraph represents the date 1-31
The fourth paragraph represents the month 1-12
The fifth paragraph represents the day of the week, 0 represents Sunday 0-6
Examples are as follows:

5 * * * ls//Specify the first 5 minutes per hour to execute the LS command 5 * * * ls//Specify 5:30 to execute the LS command per day
7 8 * ls//Specify 7:30 minutes of 8th per month to execute the LS command 7 * * * Root
run-parts/etc/cron.daily//Daily 7:50 execute all executables in the/etc/cron.daily directory as root

New Cron Task

It is recommended to add a custom task using the CRONTAB-E command (edit the cron file for the user under/var/spool/cron, the crontab file under/var/spool/cron cannot be created directly or modified directly, The crontab file is obtained through the crontab command). crontab-e

1) Execute command line directly
Print a string "Hello world" every 2 minutes, save to file/home/laigw/cron/helloworld.txt, cron format as follows:
/2 * * * echo "Hello World" >>/home/helloworld.txt
2) Shell file
The/home/laigw/cron/test.sh file is called every 3 minutes, and the cron format is as follows:
/3 * * */home/laigw/cron/test.sh
The contents of the file/home/laigw/cron/test.sh are as follows:
———————————————————————————————————————–
1
2
3 !/bin/sh

Cd/home/laigw/cron
echo "Shell" >> shell.txt
———————————————————————————————————————–
3) PHP file
PHP files need to add the command line "#!/usr/local/php/bin/php", its "/usr/local/php/bin/php" means that you need to introduce the PHP program installation directory under the/bin/php file (the Ubuntu system, The installation directory for the PHP program is/usr/local/php), and this introduction file is handled in two ways.
A. In the PHP file to introduce the command line (recommended), cron format is as follows:
/1 * * */home/laigw/cron/test.php
The contents of the file/home/laigw/cron/test.php are as follows:
———————————————————————————————————————–
1 !/usr/local/php/bin/php

———————————————————————————————————————–
Note: The test.php file needs to be given executable permissions: # chmod +x test.php
B. The command line is introduced when writing cron tasks, and the cron format is as follows:
/1 * * */usr/local/php/bin/php/home/laigw/cron/test.php
The contents of the file/home/laigw/cron/test.php are as follows:
———————————————————————————————————————–

———————————————————————————————————————–
Note: The test.php file needs to be given executable permissions: # chmod +x test.php
5. Other
/var/spool/cron/the cron service for all users in this directory
/var/log/cron Logging log information for cron runs
6. The crontab of a super user
1
2
3
4
5 Run the ' atrun ' program every minutes

#This runs anything that's due to run from the ' at '. See mans ' at ' or ' atrun '.
0,5,10,15,20,25,30,35,40,45,50,55 * * * */usr/lib/atrun
7 * * * updatedb
8,10,22,30,39,46,54,58 * * * */bin/sync
7. Example
5 * * * Root/sbin/init 6 This configures the system to automatically restart every morning at 5 o ' 30.
0/2 * */sbin/service httpd restart means to restart Apache every two hours
7 * * */sbin/service sshd start means SSH service is opened 7:50 every day
* * * */sbin/service sshd stop means the SSH service is closed 22:50 every day
0 0 1,15 * * fsck/home monthly 1th and 15th check/home disk
1 * * * */home/bruce/backup the first minute of every hour to execute/home/bruce/backup this file
* * 1-5 find/home ". xxx"-mtime +4-exec rm {}/; Every Monday to Friday 3 o'clock, in the directory/home, look for files with a. xxx file name and delete the files 4 days ago.
6/10 * ls means monthly 1, 11, 21, 31st Yes 6:30 execute the LS command
3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
This statement will run the Tar czf/usr/local/backups/daily/etc.tar.gz/etc command at 3:12 A.M. (03:12) every day. >>/dev/null 2>&1 indicates that all standard output is sent to/dev/null (the Linux Recycle Bin) and the standard error output (2) is sent to the same place (i.e./dev/null) as the standard output (1). Running this line of command will not produce any output.
This statement can become slightly more complex:
6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
It will run the tar czf/usr/local/backups/daily/etc.tar.gz/etc command in 15:30 on June 13 Monday.
The following statements can achieve the same effect:
June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
If you want to run a program in the 15th minute of every hour as a user, Joey, you can use:
* * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
where the asterisk (*) is a wildcard character, which means Cron ignores this field.
If you want to run a program every two hours, you can use */2 in the hour field. It will be at 2, 4, 6 o ... 22 O'Clock, run at 24 o '. The specific statements are as follows:
0/2 * * Joey/usr/bin/somecommand >>/dev/null 2>&1
You can also use a comma (,) to specify multiple times in a cron statement. For example, if you want to run a program at 15 minutes and 30 minutes per hour, you can use 15, 30 in the minute field:
15,30 * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
If you want to run a program in the first week of the month (that is, number 1th to 7th), you can use 1-7 in the Date field:
15,30/2 1-7 * Joey/usr/bin/somecommand >>/dev/null 2>&1
This statement will be on the 第1-7 day of the month every two hours of 15 minutes and 30 minutes (02:15,02:30 ... 22:15,22:30, etc.) run the/usr/bin/somecommand command.
If you want to execute a collection of scripts at 16:18 per day, you can put all the scripts you want to execute into a directory (such as/home/username/cron), which you can use:
* * * * root Run-parts/home/username/cron >>/dev/null 2>&1
If you want to save the output of a program, you can replace the >>/dev/null 2>&1 with the >>

If you use the SH script you also need to modify the Sh script permissions chmod +x/.../.../test.sh

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.