Source: https: //www.cnblogs.com/sunshine-H/p/9336960.html
A, Linux system installation services crontab
1. Make sure the crontab is installed
# crontab -l
If the message "command not found", it shows that the service is not installed crontab
If the message "no crontab for root", it means the service is already installed crontab, skip step one, as for the prompt solution appear please read on.
2. Install crontab Service
centos system, execute the following command:
// vixie-cron cron package is the main program;
# yum install -y vixie-cron
// rontabs software package is used to install, uninstall, or list the program used to drive the cron daemon table
# yum install crontabs
// start the service
# /sbin/service crond start 或者 systemctl crond start
// added at startup
# chkconfig --level 35 crond on
ubuntu, debian system, execute the following command:
// install crontab Service
# apt-get install cron
// start crontab service (as well as service cron stop to stop the service, service cron restart to restart the service)
# service cron start
3. Test whether the installation was successful
Continue to implement the above first command: crontab -l
Then there will be "no crontab for root" This is because you have not created any scheduled task or command wrong, that does not use crontab -e command to create any task. But here, you basically install it. crontab command details see the next step
Two, crontab command Detailed
1.crontab command there are three parameters:
-e: Edit the user's crontab
-l: List the contents of the user's crontab
Delete the contents of the user's crontab: -r
2. Function: Set the timer
Supplement: cron is a permanent service, which provides a timer feature that allows users to execute commands or preset program at a particular time. As long as a user can edit the timer configuration file, you can use the timer function. Its configuration file format is as follows:
Syntax: crontab [-u <user name>] [Profile] or crontab [-u <user name>] [- elr]
parameter:
-e to edit the user's timer settings.
-l Lists the user timer settings.
-r Remove the user timer settings.
-u <user name> Specifies the user name you want to set the timer.
3.crontab -e command Detailed
# crontab -e
If the first run the above command, let you choose editor, but some terminals will help you select the default nano editor, usually we will modify the editor vim, see Step three specific methods.
Then opens a crontab file, and then click on the i input mode, input about content:
// test.php executed once every minute
# * * * * * php /var/test.php
After the input to save and exit,: wq. Then restart the crontab Service
// different server commands are not the same, details a step
# service cron restart
4.crontab -l
View crontab which perform regular tasks, where there was nothing mentioned disadvantages.
5.crontab format
The basic format: * * * * * command
Format Description: * above represent different points of time, specifically ordered as follows: time-moon and weeks.
E.g:
* * * * * php /var/www/hello/index.php >> /tmp/log/index.txt
// represents executed once per minute, the whole sentence means that the implementation of per minute using php execute index.php file and print to /tmp/log/index.txt file specific path it ourselves
Here plus tips on viewing a file, but do not want vim <file name>. You can use cat <filename> command
1. * */2 * * * php /var/www/index.php >> /tmp/index.txt
2. * * */1 * * php /var/www/index.php >> /tmp/index.txt
// If you want to set how often do not perform, then written as "* / number", in which position represents how often executed once. The above command represent:
1. executed once every two hours
2. executed once every other day
Third, modify the default editor to vim crontab
By default 1.debian terminal editor nano, such as crontab -e will open nano, the editor favors them very accustomed to, want to modify the vim, of course, your debian system must be installed vim. If you have already installed vim, please enter the following command:
# update-alternatives --config editor
Then select /usr/vim/vim.basic on it, and the difference vim.tiny vim.basic is vim.basic for the full version of vim, and vim.tiny differ as a simplified version of the function, if not supported highlight and so on.
Enter the number 2, press Enter to open crontab -e again.