Install and use the crontab service on centOS

Source: Internet
Author: User

Enter the following command to check whether the crontab service is installed.

Which crontab

If it is already installed, the path is displayed. If it is not, the installation is very simple. Use yum to install crontab:

Yum install vixie-cron
Yum install crontabs

The vixie-cron package is the main program of cron, and the crontabs package is used to install, uninstall, or list the tables used to drive the cron daemon process.

The following two simple commands help you view and start the crond service:

View crontab service status: service crond status

Start the crontab service manually: service crond start

How can I check whether the crond service is started:

Chkconfig -- list | grep crond

If you do not know the service name or want to view all the boot services, enter chkconfig.

Basic usage:

1. crontab-l
List the current crontab task
2. crontab-d
Delete the current crontab task
3. crontab-e (crontab-r above solaris5.8)
Edit a crontab task and ctrl_D ends.
4. crontab filename
Use filename as the crontab task list file and load it

Crontab file format:
Rows in the crontab file are composed of six fields. Different fields are separated by spaces or the tab key. The first five fields specify the time when the command will be run.
Minutes (0-59)
Hour (0-23)
Date (1-31)
Month (1-12)
Day of the week (0-6, 0 indicates Sunday)
The 6th fields are strings to be executed at the appropriate time

Example:

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 *** date (solaris 5.8 does not seem to support this method)
# Every two hours from PM to AM, AM
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date


When using crontab, note that the environment variables that can be accessed in the running script are not necessarily the same as those in the current test environment, it is safer to set the environment variable (export) in the running script program)

(1ndcreate a file named crond.txt as follows and restart it at 05:36 every morning.
36 5 * reboot

(2) upload to the/opt directory

(3) run the command

Crontab/opt/crond.txt
Crontab-l

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

Service crond status: check the cron service status. If it is not started, service crond start starts it. The cron service is a scheduled service. You can use the crontab command to add or edit tasks that require scheduled execution.

========================================================== ================

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:

Add:

/Sbin/service crond start

========================================================== ==========

After referring to the above information, I wrote a test program to verify my understanding. The process is as follows and is only for reference:

Platform: EL3 User: eric

1) create a shell run. sh with the following content:

Echo "OK, crontab"

Date> ~ /Crontab_test.txt

Exit 0

2) crontab-e

Enter */1 ****~ /Run. sh

OK. Every minute, shell prompts you to receive an email.

Cat/var/spool/mail/eric | less: the execution information is displayed.

If the system stops sending emails, you can change the number to */1 ***** in step 2 ****~ /Run. sh>/dev/null

Cat crontab_test.txt


How to regularly execute php scripts in Linux

There are two scenarios:
First case: Your php script does not introduce other php files, that is, php files without the include, require_once and other commands
**/1 ***/usr/local/php/bin/php/var/www/html/wwwroot/web/script. php (script. php is executed every hour)
Note: In this case, PHP needs to be converted into executable script files. In the command line, the directory where the script is located
Chmod + x script. php
Note: the script. Php file must be added at the beginning :#! /Usr/local/php/bin/php-q. The following is the complete script. php file code.


<? Php
#! /Usr/local/php/bin/php-q
Session_start ();
Define ("DB_PASSWORD", "111111"); // server login password
Define ("DB_NAME", "db_shopping"); // database name
Define ("DB_USER", "root"); // server login user
Define ("DB_HOST", "127.0.0.1"); // server IP address or name
Define ("DB_PORT", "3306"); // server login port number
Define ("DB_CHARSET", "utf8"); // default database language
Define ("DB_DEBUG", false); // whether to set it to debug mode
Define ("DB_CLOSE", false); // whether to close 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', '2017-08-11 ', '1 ')";
$ Resl = mysql_query ($ sqll );
Echo "success ...";
?>

/Usr/local/php/bin/php indicates: use the php.exe file in the/usr/local/php/bin/directory to execute/var/www/html/wwwroot/web/script. php

Case 2: You have introduced other php files in your php script, that is, the php files that are useful for the include and require_once commands (write the shell script first)
The above method has a problem: when the script contains an include file, the script cannot find the PHP path at all. php will find the file in the/bin/local/php directory, of course not found.
In this case, follow these steps:
Enter the following command on the terminal to obtain the path of the sh and lynx programs:
Whereis sh
Whereis lynx
Or
Which sh
Which lynx
If you enter one, the path you want will be returned. If your system does not have lynx, try links, w3m, or links2. These are other text model browsers. These browsers will run your scripts.
Then you need to create a sh file and use it to call the php file through lynx. Assume that your server has installed lynx, the sh path is/bin/sh, and the lynx path is/usr/local/bin/lynx.

#! /Bin/sh
/Usr/local/bin/lynx-source http://www.yoursite.com/folder/script.php
Note the exclamation point
Save this script as a cron-lynx.sh
Save it to a location you can find, for example:

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 crontab of the host, as shown below.
*/1 */usr/www/yourdirectory/sh_scripts/cron-lynx.sh
There is a space between/bin/sh and/usr. In this case, it is set up. Note that your sh and PHP files are correct.

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.