Linux CentOS Timer Run Script Configuration Method
- 2021-06-28 14:44:40
- OfStack
Many times we want the server to run a script regularly to trigger an operation, such as uploading with 7-bull tools. If a new file is added to the synchronization file, we can provide a timer script to complete the synchronization commands we need (7-bull qrsbox tools are automatically synchronized.Simply add a file to the sync folder to automatically listen for uploads).
1. Install crontab
[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs
The vixie-cron package is the main program of cron;
The crontabs package is a program used to install, uninstall, or list tables that drive the cron daemon.
2. Open crontab Service
service crond start // Start Services
Start and close this cron service in the following ways:
service crond start //Start Service
service crond stop //Shutdown Service
service crond restart //Restart Service
service crond reload //Reload Configuration
View crontab service status: service crond status
Manually start the crontab service: service crond start
To see if the crontab service is set to start on, execute the command: ntsysv
Join Startup Autostart:
chkconfig level 35 crond on
Also included are the ntsysv and chkconfig commands:
About ntsysv command is a class GUI management mode to set up boot-up, it needs to be installed before it can be used. After yum install - y ntsysv is installed, only one ntsysv needs to be run to appear a similar graphical management interface.
Up and down keys: can move between services in the middle box;
Spacebar: can be used to select the service you want, [*] means start up;
tab key: can move between box, OK, Cancel;
[F1] Key: The description of the service can be displayed.
The format of the chkconfig command line sets whether a service starts on its own or queries how a service runs at six startup levels.
Set boot-up to automatically start crond service:
[root@CentOS ~]# chkconfig crond on
See how the crond service is running at each boot level
[root@CentOS ~]# chkconfig list crond
crond 0:Close 1:Close 2:Enable 3:Enable 4:Enable 5:Enable 6:Close
You can see that level 2, 3, 4, 5 startup automatically starts the crond service
Cancel startup of crond service automatically:
[root@CentOS ~]# chkconfig crond off
3. Set up scripts to execute
There are two new ways to schedule tasks:
1) Enter at the command line: crontab-e and add the corresponding task, wq save exits.
2) Edit/etc/crontab file directly, that is, vi/etc/crontab, and add corresponding tasks.
crontab-e configuration is for a user, while editing/etc/crontab is a system-specific task
View Scheduled Tasks
crontab-l //List all current scheduled tasks
crontab-l-u jp //Lists all scheduling tasks for user jp
Delete Task Scheduling
crontab-r //Delete all task schedules
Edit vim/etc/crontab directly. The default file format is as follows:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
This text is already more intuitive to interpret, where
Asterisk (*): Represents all possible values, for example, if the month field is an asterisk, then the command action is executed monthly when the constraints of other fields are met.
Comma (,): You can specify a list range with comma-separated values, for example, "1,2,5,7,8,9"
Middle bar (-): You can use the middle bar between integers to represent an integer range, for example, "2-6" for "2,3,4,5,6"
Forward slash (/): You can specify the interval frequency of time with forward slash, for example, "0-23/2" means that it is executed every two hours.Also forward slashes can be used with asterisk 1, for example */10, which means that they are executed every 10 minutes if used in the minute field.
Here are a few examples, covering a few common cases:
Instance 1
5 * * * * ls Specify the hour number 5 Minute execution 1 second ls command
30 5 * * * ls Specify daily 5:30 implement ls command
30 7 8 * * ls Specify a month 8 Numbered 7 : 30 Sub-execution ls command
30 5 8 6 * ls Specify yearly 6 month 8 day 5 : 30 implement ls command
30 5 8 6 * ls Specify yearly 6 month 8 day 5 : 30 implement ls command
30 6 * * 0 ls Specify Sunday 6:30 implement ls command
30 3 10,20 * * ls monthly 10 Number and 20 Numbered 3 : 30 implement ls command
25 8-11 * * * ls Every day 8-11 Point number 25 Minute execution ls command
*/15 * * * * ls each 15 Minute execution 1 second ls command
30 6 */10 * * ls Every month, every other 10 day 6:30 implement 1 second ls command
22 4 * * * root run-parts /etc/cron.daily
# Every day 4 : 22 with root Identity Execution /etc/cron.daily All executable files in the directory, run-parts The parameter represents the execution of all executables in the following directory.
Instance 2
# Every night 21:30 restart apache
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
# monthly 1 , 10 , 22 Daily 4 : 45 restart apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
# weekly 6. Sunday 1 : 10 restart apache
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
# Every day 18 : 00 to 23 : 00 Every other 30 Minute Restart apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
# Every week 6 Of 11 : 00 pm restart apache
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
# Night 11 From o'clock to morning 7 Between points, every other 1 Hour Restart apache
0 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
# each 1 Hour Restart apache
0 */1 * * * /usr/local/etc/rc.d/lighttpd restart
# Monthly 4 Number and Weekly 1 To Week 3 Of 11 Point Restart apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#1 month 1 Numbered 4 Point Restart apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
# Synchronize every half hour 1 Next Time
0/30 * * * * /usr/sbin/ntpdate 210.72.145.44
Be careful
The * *1 * * * command means that it is executed every minute of the hour.
Execution must be specified at the minute of each hour, that is, the first * number must be changed to a numeric value.
Because the * sign means every minute.
The other hour/1 is the same as no difference, once an hour.
If you set */2, it is actually the number of hours divisible by 2 instead of 2 hours from the start of the timer, such as 9 o'clock to 10 o'clock.
You may end up with the following problem
root User Enter crontab-l Display
no crontab for root For example:
[root@CentOS ~]# crontab -l
no crontab for root
This problem is very simple, also enter crontab -e under root user
Press Esc: wq Enter
No problem entering crontab-l
The main reason is that this liunx server used crontab for the first time and did not generate a corresponding file, which was generated after editing (crontab - e)