Linux scheduled task system cron

Source: Internet
Author: User
Tags html header
Cron is a Linux scheduled execution tool that can run jobs without human intervention. Since cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration
You can also enable the service automatically when the system starts:
/Sbin/service crond start
Now that the cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:
1. directly use the crontab command to edit
The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // Delete the cron service of no user
Crontab-E // edit a user's cron Service
For example, to view your cron settings as root: crontab-u root-l
For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R
When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.
In VI editing mode, the edited content must conform to the following format: */1 ***** LS & gt;/tmp/ls.txt
The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:
Minutes (0-59)
Hour (0-23)
Date (1-31)
Month (1-12)
Week (0-6) // 0 represents Sunday
In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:
Every morning
0 6 *** echo "Good morning. "& gt;/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.
Every two hours
0 */2 *** echo "have a break now." & gt;/tmp/test.txt
Every two hours from PM to am, am
0 23-7/2, 8 *** echo "have a good dream :)" & gt;/tmp/test.txt
Am from Monday 4 to Wednesday every week
0 11 4*1-3 command line
Am, January 1, January 1
0 4 1 1 * command line
After a user's cron settings are edited, cron automatically generates a file with the same name under/var/spool/cron, the cron information of this user is recorded in this file. This file cannot be edited directly, but you can use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.
2. Edit the configuration cron in the/etc/crontab file.
The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:
Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.
Home = // path of the user running. The root directory is used here.
# Run-Parts
01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. Weekly // execute the script in/etc/cron. Weekly every week
42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.
Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.
Source: chinalinuxpub

//// // In Linux, crontab is a command for scheduled execution, with this command, we can regularly execute a PHP file to implement automatic script execution in Linux using PHP. How to Use PHP for Linux automatic script execution? When installing PHP, an executable file named PHP is generated. copy it to/usr/local/bin. run the PHP program in the terminal mode: PHP-Q onlinnum. PHP
PHP was originally applied on a webpage, so it will send the HTML header, but here we want to use PHP as a shell script, "-Q"
It means not to send the header. You can try not to add the-Q display result. Now you can execute PHP code in the terminal mode.
Linux Command: cron daemon is a resident service in the system. It performs routine work, such as checking disks once a day or once a month. Cron Daemon checks the scheduled Worksheet (crontab) every minute to see if there are instructions to be executed. All outputs are sent to the user by mail. Set the crontab command: crontab-E. This command calls the VI editor to edit the executed list. For example, 0 0, ** fsck/Home 1 *****/home/myhome/printhello indicates a scheduled job, which is scheduled before the command, there are a total of five columns separated by spaces, from left to right in order as follows: ------------------------ minute from 00 to 99 o'clock from 0 to 24 from 01 to 31 months from 01 to 12 weeks from 01 to 07, representing Monday to Sunday * indicates "every ", for example, if the task is executed on a daily basis, enter the * No. ---------------------- in the third column. Therefore, the two tasks in the preceding example are: check the disk number 1 and the number 15 of each month. Run the/home/myhome/printhello file to view crontab: crontab-l Delete crontab: crontab-r we will know how to use the PHP and crontab commands for Linux to automatically execute scripts. What else do you need to pay attention? If a database is used in PHP, such as Oracle, you also need to set the export Oracle environment in the execution script. Because the script execution in cron is performed by users without logging on to the database, many environment variable values set in the system are missing, and problems may occur when connecting to the database.
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.