Crontab configuration run php

Source: Internet
Author: User
Tags pkill mrtg
Allow crontab to automatically schedule rows of PHP

To use the crontab auto-dynamic PHP program, you can do this:

Method 1
1. Add the following in the first line of the PHP program:

#! /Usr/local/PHP/bin/PHP-Q

For example:

Plain textPHP:

  1. #! /Usr/local/PHP/bin/PHP-Q
  2. <? PHP
  3. $ Foo = 123;
  4. ?>

Please note that my PHP is installed in/usr/local/PHP. Please modify the location of your PHP installation line at the root.

2. Restrict the PHP program to users:

Chmod + X testing. php

3. Click crontab-E in the rows and then add the following content:

00 00 ***/path/to/testing. php>/dev/null 2> & 2

The above method will parse the line/path/to/testing. php at every day.

Method 2
Another way is to avoid adding "#! /Usr/local/PHP/bin/PHP-Q ", you can omit Step 1 and step 2, directly click crontab-E, and merge the following content:

00 00 *** usr/local/PHP/bin/PHP-Q/path/to/testing. php>/dev/null 2> & 2

The result of this method is the same as that of method 1.

 

Cron configuration and description

Author:North South
From:Linuxsir. org
Abstract:In a Linux operating system, scheduled tasks are usually undertaken by cron. Understanding or understanding cron can help us manage servers more conveniently and save more time. This article also provides some simple and clear examples to facilitate new users;

Directory

    1. Summary of scheduled tasks;
    2. cron;

      2.1 install and start cron;

        2.1.1 installation of RPM package management systems such as RedHat or fedora;
        2.1.2 installation in the slackware system;
        2.1.3 how to enable, stop, and restart cron;

      2.2 cron configuration file;

        2.2.1 global configuration file;
        2.2.2 cron configuration plan task writing format
        2.2.3 definition method for executing multiple tasks at the same time;
        2.2.4 description of the user's cron configuration file;

    3. Application Scope of scheduled tasks;
    4. About this article;
    5. postscript;
    6. references;
    7. related documents;


++
Body
++


1. Summary of scheduled tasks;

A scheduled task is a task that executes the scheduled job at the agreed time. In Linux, we often use the crond server to do this. The cron server can perform specific tasks according to the time specified in the configuration file. For example, we can schedule a restart of the httpd server at every morning in the configuration file. This is a scheduled task;

Let's talk about cron first;


2. cron;

In Linux, scheduled tasks are generally undertaken by cron. We can set cron to automatically start upon startup. After cron is started, it reads all its configuration files (global configuration file/etc/crontab and the scheduled task configuration file for each user ), then, cron calls the job on time based on the command and execution time.


2.1 install and start cron;

In general, cron is installed by default. We need to check whether the software package has been installed;


2.1.1 installation of RPM package management systems such as RedHat or fedora;

Determine whether the system has installed Cron-related packages

The situation of the fedora 5.0 system;

Other versions of fedora and RedHat systems are similar to this;

[root@localhost ~]# rpm -qa |grep cron
vixie-cron-4.1-54.FC5
anacron-2.3-36.1
crontabs-1.10-7.1

The vixie-cron package is the main program of cron. You can use the following command to understand it. The same applies to other software packages;

[root@localhost beinan]# rpm -ql vixie-cron

The crontabs package is used to install, uninstall, or list the tables used to drive the cron daemon. The cron daemon checks the crontab file to view the scheduled execution time of a specified command. If the commands have been scheduled, the daemon will execute them. To understand what the crontabs software package has installed, use the following command;

[root@localhost beinan]# rpm -ql  crontabs

If no software package is installed, you can find a software package with the cron name in the file name from the disc for installation.

[root@localhost ~]# rpm -ivh vixie-cron-4.1-54.FC5*
[root@localhost ~]# rpm -ivh crontabs*

You can also use yum for online installation;

[root@localhost ~]# yum install vixie-cron
[root@localhost ~]# yum install crontabs


2.1.2 installation in the slackware system;

In slackware, the cron package is dcron. You can find it on the disc and install it;

Check whether the dcron package has been installed. The installation details of the software package can be listed here;

[root@localhost ~]# more /var/adm/packages/dcron*

If not, run the following command. Find the dcron package in the first installation disk;

[root@localhost ~]# installpkg dcron*.tgz


2.1.3 how to enable, stop, and restart cron;

After the cron configuration file is modified, you must re-use the crond server to make the new file take effect. There are two options;


First, in systems managed by rpm packages such as fedora or RedHat;

[root@localhost ~]# /etc/init.d/crond start
[root@localhost ~]# /etc/init.d/crond stop
[root@localhost ~]# /etc/init.d/crond restart

If you want to create a clone release in fedora, RedHat, or based on these releases, use the following method if you want to automatically start the instance;

[root@localhost ~]# chkconfig --levels 35 crond on

Second: General

[Root @ localhost ~] # Pgrep crond Note: Determine whether crond is running;
[Root @ localhost ~] # Pkill crond Note: Kill the running crond;
[Root @ localhost ~] # Pgrep crond Note: Check whether the object has been killed;
[Root @ localhost ~] #/Usr/sbin/crond Note: Run crond;


2.2 cron configuration file;

Cron is a server program. We all know that Linux server configurations are mostly completed through configuration files, and cron is no exception, in systems such as RedHat and fedora, the global configuration file is/etc/crontab. In slackware systems, the global file is/var/spool/cron/crontabs/root. Each user also has his own cron configuration file, which can be edited through crontab-E;


2.2.1 global configuration file;

The following is an example of a global configuration file, taking Fedora Core 5 as an example;

[root@localhost ~]# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
1 * * * * root run-parts /etc/cron.hourly
#
2 4 * * * root run-parts /etc/cron.daily
#
22 4 * * 7 root run-parts /etc/cron.weekly
#
42 4 1 * * root run-parts /etc/cron.monthly
#
0,5,10,15,20,25,30,35,40,45,50,55 * * * * root /usr/bin/mrtg /etc/mrtg/mrtg.cfg

For the global configuration file, we must use the root permission to change it, usually through crontab-e. But sometimes this is not the case. For example, if Fedora Core 5 modifies the cron configuration file of the root user through crontab-E. Therefore, you can use the VI editor to modify the global configuration file in Fedora Core 5. For the usage of VI, see:File editor vi

The global configuration file defines the PATH environment variables for executable commands and the definition of the shell type used. Most of these variables do not need to be modified; we often add scheduled tasks or modify the task execution time;

In the global configuration file, we found that there are rows similar to these;

1 * root run-parts/etc/cron. Hourly Note: Execute executable programs or scripts in the/etc/hourly directory in the first minute of every hour;
2 4 * root run-parts/etc/cron. daily Note: Execute executable programs or scripts in the/etc/cron. daily directory at 04:02 every day;
22 4 ** 7 root run-parts/etc/cron. weekly Note: At every 7th weeks, I start to execute/etc/cron. executable programs or scripts in the weekly directory;
42 4 1 ** root run-parts/etc/cron. Monthly Note: Execute executable programs or scripts in the/etc/cron. Monthly directory at 04:42 on the seventh day of every month;

These rows are used to execute tasks in batches at the same time. We will explain them independently in future examples. Here we will only let everyone know;


2.2.2 cron configuration plan task writing format

Minute hour, day month week [user name] command

Note:

The first section should be defined as minute, which indicates the minute of every hour for execution. The range is from 0 to 59. The second part should be defined as hour, which indicates execution from the hour. The range is from 0 to 23. The third part should be defined as date, it indicates that the task is executed from the day of each month. The value range is from 1-31 to the fourth segment. The value range is from 1-12 to the fifth segment: week indicates the day of the week, ranging from 0 to 6, where 0 indicates Sunday. The user name, that is, the user in which the execution program is to be executed, should be defined in each section 6. This can be omitted in general. The seventh segment should be defined as the command and parameter to be executed.

Note:The user name is omitted. the user name defines which user is used to execute the program. For example, for MySQL server, we can define the user name to start, stop, and restart mysql users; but it doesn't make much sense for cron, because every user has its own cron configuration file. Some programs must use the root user to start. In this case, we can modify the cron configuration file of the root user. You do not need to specify the user name in the configuration file of each user.

We can write the scheduled task in the global configuration file. If you want to put a plan into the global configuration file, you have to change the cron global configuration file corresponding to the release version, for example, the global configuration file of Fedora 5 is the/etc/crontab file;

Each user can also define their own cron configuration file, which can be defined using the crontab-e command;

For example, restart the machine at 08:30 every day;

About timeThe format contains seven fields. We can directly change or add it. Of course this is a system task call. For example, if I restart the machine at 08:30 every day
Add the following two sentences to/etc/crontab. The first sentence is the comment. Start with # And write a note that you can know what the task is;

# reboot OS
30 8 * * *  root /sbin/reboot

The first section should be defined as minute, which indicates the minute of every hour for execution. The value range is from 0 to 59.
The second part should be defined as hour, which indicates execution from hour to hour. The value range is from 0 to 23.
The third paragraph should be defined as: date, which indicates execution from the day of each month, range from 1-31
The fourth part should be defined as month, which indicates the number of months of the year to be executed, ranging from 1 to 12.
The fifth part should be defined as Week, which indicates the day of the week for execution, ranging from 0 to 6, where 0 indicates Sunday.
The user name should be defined in each section, that is, the user in which the execution program is to be executed, which can be omitted;
Section 7 should define the commands and parameters to be executed.

Compared with the above example, do you know 30 minutes ?? 8 is it an hour? If * represents all, that is, every month, every day, every week. The root command is run by the root user. The command is/sbin/reboot. That is to say, the system restarts at 08:30 every day;

We can write the scheduled task restarted at 08:30 every day into the cron global configuration file, or define it in the root user's cron configuration file. If it is defined in the root configuration file, use the root identity to execute crontab-e to modify the configuration file. crontab-e enters the configuration file modification process, which is actually the same as that of VI, see VI usage:File editor vi

Make the configuration file take effect:If the configuration file takes effect, you have to restart cron. Remember that since the cron configuration file under each user is modified. Restart the cron server.

In fedora and RedHat, we should use;

[root@localhost ~]# /etc/init.d/crond restart

If you want crond to run at startup, you should change its running level;

[root@localhost ~]# chkconfig --levels 35 crond on

In slackware, if it runs automatically upon startup;

Check whether the/etc/rc. d/rc. M file is like a row. If not, add it. Most of the rows are available;

# Start crond (Dillon's crond ):
If [-x/usr/sbin/crond]; then
/Usr/sbin/crond-L10>/var/log/cron 2> & 1
</Cdoe>

<B>
If you want to immediately enable the cron restart to take effect, you can also use the following method;
</B>

<Code>
[Root @ localhost ~] # Pgrep crond Note: Check whether the crond server is running;
2022
[Root @ localhost ~] # Pkill crond Note: Kill crond;
[Root @ localhost ~] # Pgrep crond Note: Check whether crond exits;
[Root @ localhost ~] #/Usr/sbin/crond Note: Start crond;
[Root @ localhost ~] # Pgrep crond Note: Check whether crond is running;
3883


2.2.3 definition method for executing multiple tasks at the same time;

As we have mentioned earlier, the global configuration file contains a section similar to the following;

1 * root run-parts/etc/cron. Hourly Note: Execute executable programs or scripts in the/etc/hourly directory in the first minute of every hour;
2 4 * root run-parts/etc/cron. daily Note: Execute executable programs or scripts in the/etc/cron. daily directory at 04:02 every day;
22 4 ** 7 root run-parts/etc/cron. weekly Note: At every 7th weeks, I start to execute/etc/cron. executable programs or scripts in the weekly directory;
42 4 1 ** root run-parts/etc/cron. Monthly Note: Execute executable programs or scripts in the/etc/cron. Monthly directory at 04:42 on the seventh day of every month;

For example, I want to restart the httpd server at 05:10 every day. At the same time, we can download an ISO file at the same time. If we write tasks one by one based on the rules of writing scheduled tasks and add them to the global environment, it will be a little troublesome. It is better to write a script in the/etc/crond. Daily directory. Set the permission to 755. For details about permission settings, refer:Linux file and directory attributes).

Of course we have to change the line with/etc/cron. daily above to the following;

10 5 * root run-parts/etc/cron. daily
</Cdoe>

Then we create two files in the/etc/crond. Daily directory. One is used to restart the httpd server, as shown below;

<code>
[root@localhost cron.daily]# touch httpd.sh
[root@localhost cron.daily]# chmod 755 httpd.sh
[root@localhost cron.daily]# echo "/etc/init.d/httpd restart" > httpd.sh
[root@localhost cron.daily]# more httpd.sh
/etc/init.d/httpd restart

If you want to download the fc5 image at 05:20 every day, you can create another file fc5down. Sh.

[root@localhost cron.daily]# touch fc5down.sh
[root@localhost cron.daily]# chmod 755 fc5down.sh
[root@localhost cron.daily]# echo "/usr/bin/wget  http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso" > fc5down.sh
[root@localhost cron.daily]# more fc5down.sh
/usr/bin/wget  http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso

Then we can restart crond;

[root@localhost cron.daily]# pkill crond
[root@localhost cron.daily]# prep crond
[root@localhost cron.daily]# crond&

Note:I just want to explain the simple usage of executing multiple scheduled tasks at the same time, instead of teaching you to download the fc5 image every morning. I will give this example to help beginners understand this function. If you have many tasks to execute at a certain time, write the script file by yourself. Put it in the cron. Hourly, cron. daily, cron. weekly, and cron. Monthly directories under the/etc directory. All executable scripts put into these directories can be accurately executed within the agreed time. Each directory has the purpose of each directory;


2.2.4 description of the user's cron configuration file;

Each user has his own cron configuration file, which can be edited through crontab-E. In general, in the release of fedora and RedHat, after the cron configuration file is saved and exited, it is automatically stored in the/var/spool/cron/directory. The file is named after the user name. Slackware is located in the/var/spool/cron/crontabs/directory. Other releases are similar. Find it by yourself. Check that the user's scheduled task is crontab-L.

Crontab-e Note: edit the user's cron configuration file;
Crontab-l note: You can view your scheduled tasks;

For example, I use the beinan user to run crontab-E;

[Beinan @ localhost ~] $ Id Note: identify the user used;
Uid = 500 (beinan) gid = 500 (beinan) groups = 500 (beinan)

[Beinan @ localhost ~] $ Crontab-e Note: edit the scheduled tasks of beinan;

Define a task in it;

40 20 * * *  /usr/bin/wget  http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso

After saving and exiting, run the following command to view the beinan user's scheduled tasks;

[beinan@localhost ~]$ crontab -l

40 20 * * *  /usr/bin/wget  http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso

After the configuration is complete, we need to restart the crond server. The crond server must be restarted for all cron configuration file changes of each user. As mentioned earlier, this is omitted. It is worth noting that the crond server is restarted, root permission is required. You can use the su command to switch to the root user, and then restart crond;


In a release version managed by rpm, such as fedora or RedHat;

[Beinan @ localhost ~] $ Su
Password:
[Root @ localhost beinan] #/etc/init. d/crond restart
Stop crond: [OK]
Start crond: [OK]


You can also first kill crond and then run the crond command to start it;

[root@localhost beinan]# pkill crond
[root@localhost beinan]# pgrep crond
[root@localhost beinan]# /usr/sbin/crond
[root@localhost beinan]# pgrep crond
6664


3. Application Scope of scheduled tasks;

Scheduled tasks are mainly for the system to automatically complete some work. For example, it is important for network administrators to enable the system to automatically clear or back up the logs of the httpd server at a certain time point and restart the httpd server. The system can also automatically clear the junk files in the/tmp directory.

It is also worth mentioning that linuxsir. org has many sub-sites, each of which has RSS, so that the RSS of these sites can be delivered to each other, and cron is also used to complete the task. For example, the task is started around 4 o'clock in the morning. This is a very resource-consuming process. If it is not done when there are few online users, it can only be done in the morning. As the Administrator of Sir, it is impossible to get up and click the mouse every morning to complete these tasks. Therefore, the best way is to use cron to automatically run the task, so that the task can be completed when the server is idle;


4. About this article;

This article is about task scheduling. I originally wanted to write all the content about the scheduled task in one document. Because I was not prepared before writing the document, I wrote only one cron; this article may be adjusted in the near future to add at and other tools;

 

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.