Linux timed Task execution

Source: Internet
Author: User

(1) How do PHP scripts execute regularly under Linux?
(2) How do I set up scheduled tasks under Linux?
(3) Crontab timed Execution Program

Core tips:
Type crontab
-e Edit crontab service file

There are two scenarios: (there is also a case where you can consider using Linux curl when you need to pass parameters)
The first case: your PHP script did not introduce other PHP files, that is, there is no include,require_once and other commands such as PHP files
* */1 * * */usr/local/php/bin/php/var/www/html/wwwroot/web/script.php (once per hour script.php)
Note that this requires that PHP be translated into an executable script file. At the command line, the directory where the script is located
chmod +x script.php
Note: Your script.php file must be added at the beginning: #!/usr/local/php/bin/php-q, Below is a complete script.php file code

<?php#!/usr/local/php/bin/php-qsession_start ();d efine ("Db_password", "111111"); Server login Password define ("Db_name", "db_shopping"); Database name define ("Db_user", "root"); Server login User define ("Db_host", ""); Server IP address or name define ("Db_port", "3306"); Server login port number define ("Db_charset", "UTF8"); Database default language define ("Db_debug", false); is set to debug mode define ("Db_close", false); Whether to actively shut down the database connection $con=mysql_connect (db_host. ":". Db_port,db_user,db_password); mysql_query ("SET character_set_connection =". Db_charset. ", Character_set_results =". Db_charset. ", character_set_client = binary"), if (! $con) die ("Mysql Error:". Mysql_error ()); mysql_select_db (db_name,$ con);//$dates =date () $SQLL = "INSERT INTO Test (name,sex,thisdate,state) VALUES (' Love ', ' 0 ', ' 2011-08-11 ', ' 1 ')"; $resl = mysql_query ($SQLL); echo "Success ...";? >

/usr/local/php/bin/php means: Use the Php.exe file in the/usr/local/php/bin/directory to execute the/var/www/html/wwwroot/web/script.php

The second case: your PHP script has introduced other PHP files, that is useful to the include,require_once command PHP file (first write shell script)
There is a problem with the method above: when there is an include file in the script script, the scripts do not find the path to PHP at all, and PHP will look for the file in the/bin/local/php directory, which of course cannot be found.
So, when you meet this situation, do this in the following way:
Enter the following command on the terminal to get the path to the SH and lynx programs:
Whereis SH
Whereis Lynx
which SH
which Lynx
Just type in one, and it will return the path you want. If you don't have lynx in your system, try links,w3m, or LINKS2, which are other text-model browsers. These browsers will run your script.
Then you need to create an SH file that uses it to invoke the PHP file via Lynx. Suppose your server has a lynx,sh path of/bin/sh,lynx path to/usr/local/bin/lynx
Notice the exclamation mark,
Save this script as
Save to a place you can find, such as:

You can put it in the root directory of the website.
Then create a script.php file. When Cron is running, it will be called through Lynx. This file can be accessed through a browser.

Then edit the host's crontab, like so.
*/1 * * * * root/bin/sh/usr/www/yourdirectory/sh_scripts/
There are spaces between/bin/sh and/usr. This is set up, and one thing to note is that your sh files and php files are correct.


crontab services under Linux:
1. Crontab is used to enable the user to execute the program at a fixed time or at a fixed interval.
If you need to implement task scheduling on a Linux platform, you can write cron scripts to implement it.
Perform a task at a certain frequency
Linux lacks the capital to start the Crond process, the Crond process does not require the user to start, shut down.
The Crond process is responsible for reading the dispatch task and executing it, and the user only needs to write the corresponding dispatch script to the Cron scheduling profile.
The Cron Scheduler file has the following several:
1. crontab
3. cron.daily
4. cron.hourly
5. cron.monthly
6. cron.weekly

If the task you are using is not performed in hourly monthly weekly mode, you can write the corresponding crontab to the crontab or CRON.D directory.
Executes the script every other minute/opt/bin/
You can create a new script in CRON.D
Content is
*/1 * * * * root/opt/bin/

2. 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, if it does not automatically get up, you can start and close the 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

See if the service is already running with Ps-ax | grep cron

3. crontab command

To see if the Crontab service under this user was created successfully, use the CRONTAB-L command
Command which PHP to view the path of PHP installed under Linux
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:

Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a cron service with no users
CRONTAB-E//Edit a user's cron service
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The Crond resident command for task scheduling Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the default is to start the
The Task Dispatch command. The Crond command periodically checks to see if there is any work to be done and the work will be performed automatically if there is work to be done.

crontab Command options:

-u Specifies a user
-l lists a user's task schedule
-R Delete a user's task
-e Edit a user's tasks

4. cron file Syntax:

Hour of the week order
0-59 0-23 1-31 1-12 0-6 Command (value range, 0 for Sunday One a row corresponds to a task)

Remember the meanings of several special symbols:

"*" represents a number in the range of values,
"/" stands for "every", such as every 2 minutes: */2
"-" represents a number to a number,
"," separate a few discrete numbers

5, the Task scheduling settings file
The CRONTAB-E command can be used to edit, edit the corresponding user's cron file under/var/spool/cron, or modify the/etc/crontab file directly.
The specific format is as follows:
Minute Hour Day Month Dayofweek command
Minutes hours days months days per week order
Each field represents the following meanings:
Minute the first few minutes of every hour to perform the task
Hour the first few hours of the day to perform this task
Day of the month to perform the task
Month months of the year to perform this task
DayOfWeek the day of the week to perform the task
Command Specifies the program to execute
In these fields, other fields are optional except that the Command is a field that must be specified each time.

field, depending on your needs. For a field that is not specified, "*" is used to fill its position.
Examples are as follows:
5 * * * ls Specifies the first 5 minutes of every hour to execute the LS command
5 * * * ls specifies the 5:30 execute LS command per day
7 8 * * ls specifies 7:30 minutes of 8th per month to execute the LS command
5 8 6 * ls specifies that the LS command be executed every June 8 5:30
6 * * 0 ls specifies 6:30 execute ls command per Sunday [Note: 0 for Sunday, 1 for Week 1,

And so on, can also be expressed in English, Sun said Sunday, Mon said Monday and so on. ]

3 10,20 * * ls 3:30 for each month 10th and 20th executes the LS command [note: "," used to connect multiple discontinuous periods]

8-11 * * * ls command is executed at 25 minutes per day at 8-11 Pips [Note: "-" used to connect a continuous period of time]

*/15 * * * * ls is executed every 15 minutes with the LS command [i.e. No. 0 15 30 45 60 minutes per hour to execute the LS command]

6 */10 * ls command is executed every 10 days each month at 6:30 (that is, 1, 11, 21, 31st is 6:30 executes the LS command once. ]

Every day 7:50 executes all executables in the/etc/cron.daily directory as root

7 * * * Root run-parts/etc/cron.daily [Note: The Run-parts parameter indicates that all executables in the following directory are executed. ]

6. New Scheduling task

There are two ways to add a dispatch task:
1), at the command line input: CRONTAB-E and then add the corresponding task, Wq save the disk to exit.
2), directly edit the/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.

7. View scheduling Tasks
Crontab-l//List all current Scheduled tasks
Crontab-l-u JP//List all scheduling tasks for user JP

8, delete task scheduling work
Crontab-r//Delete all task scheduling work

9. The steering of task scheduling execution results
Example 1: Execute the LS command 5:30 every day and output the results to the/jp/test file
5 * * * ls >/jp/test 2>&1
Note: 2>&1 indicates execution results and error messages.

Edit/etc/crontab File Configuration cron
Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something.
The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:



Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email

home=///user-run path, here is the root directory
# Run-parts

* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly

4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily

4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly

4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly
Attention to the "run-parts" parameter, if you remove this parameter, you can write a script name to run, instead of the folder name

For example:
1) in the command line input: Crontab-e then add the corresponding task, Wq save the disk to exit.
2) Edit the/etc/crontab file directly, i.e. Vi/etc/crontab, add the corresponding task

10. Plan to run your PHP script by mistake
Because PHP scripts may have an absolute path containing (require,include) and relative paths, it is best to first CD before executing. As follows:

*/10 * * * * Cd/var/www/cron &&/usr/bin/php/var/www/cron.php

Linux timed Task execution

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.