Linux CentOS regularly run script configuration method

Many times we hope that the server will run a script to trigger an operation on a regular basis, such as uploading using Qiniu’s tool. If a new file is added to the synchronization file, at this time we can provide a timing script to complete the synchronization command we need (7 Niu's qrsbox tool will automatically synchronize, just add a file in the synchronization folder and it will automatically monitor the upload).

1. Install crontab

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

The vixie-cron software package is the main program of cron;

The crontabs package is a program used to install, uninstall, or list tables used to drive the cron daemon.

2. Turn on crontab service

service crond start //Start service

Use the following methods to start and close the cron service:

service crond start //Start the service

service crond stop //Close the service

service crond restart //Restart the service

service crond reload //reload configuration

View crontab service status: service crond status

Manually start the crontab service: service crond start

Check whether the crontab service has been set to start at boot, execute the command: ntsysv

**Join auto start after boot: **

chkconfig –level 35 crond on

In addition, by the way, introduce the ntsysv and chkconfig commands:

About the ntsysv command is a graphical interface management mode to set the boot up, you need to install it before you can use it. After yum install -y ntsysv is installed, you only need to run an ntsysv to appear a graphical management interface.

Up and down keys: you can move between services in the middle box;

Space bar: can be used to select the service you need, [*] means to start;

Tab key: can move between box, OK, Cancel;

[ F1] key: can display the description of the service.

Regarding the form of chkconfig command line, whether to auto-start or query the operation of a certain service at 6 boot levels.

**Set the crond service to start automatically after booting: **

[ root@CentOS ~]# chkconfig crond on

View the operation of the crond service at each boot level

[ root@CentOS ~]# chkconfig –list crond

crond 0: off 1: off 2: enabled 3: enabled 4: enabled 5: enabled 6: disabled

You can see that the crond service will be started automatically when the level 2, 3, 4, and 5 are opened.

**Cancel the automatic start of crond service after booting: **

[ root@CentOS ~]# chkconfig crond off

3. Set the script to be executed

There are two ways to add scheduling tasks:

1)、 Enter in the command line: crontab -e and then add the corresponding task, wq save and exit.

2)、 Directly edit the /etc/crontab file, vi /etc/crontab, and add the corresponding tasks.

The crontab -e configuration is for a certain user, and editing /etc/crontab is a task for the system

View scheduled tasks

crontab -l //List all current scheduled tasks

crontab -l -u jp //List all scheduled tasks of user jp

Delete task scheduling work

crontab -r //Delete all task scheduling tasks

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 ofmonth(1-31)
# |||.- - - - - - - month(1-12) OR jan,feb,mar,apr ...
# ||||.- - - - day ofweek(0-6)(Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |||||
# ***** user-name command to be executed

**This text explains it more intuitively, where **

Asterisk (*): Represents all possible values. For example, if the month field is an asterisk, it means that the command operation will be executed every month after meeting the constraints of other fields.

Comma (,): You can specify a list range with comma-separated values, for example, "1,2,5,7,8,9"

Middle bar (-): A middle bar between integers can be used to represent an integer range, for example, "2-6" means "2,3,4,5,6"

Forward slash (/): You can use forward slashes to specify the time interval frequency, for example, "0-23/2" means that it will be executed every two hours. At the same time, the forward slash can be used with an asterisk, such as */10. If it is used in the minute field, it means that it will be executed every ten minutes.

Here are a few examples, which basically cover some common situations:

Example 1

5**** ls specifies that the ls command is executed every 5 minutes
305*** ls specifies 5 every day:30 execute ls command
3078** ls specifies that the ls command is executed at 7:30 on the 8th of each month
30586* ls specifies that the ls command is executed at 5:30 on June 8th every year
30586* ls specifies that the ls command is executed at 5:30 on June 8th every year
306**0 ls specifies 6 every Sunday:30 execute the ls command
30310,20** ls Run the ls command at 3:30 on the 10th and 20th of each month
258- 11*** ls 8 per day-Execute the ls command at the 25th minute at 11 o'clock
* /15****ls execute the ls command every 15 minutes
306* /10**ls Every month, every 10 days 6:30 execute the ls command once
224*** root   run-parts   /etc/cron.daily
# Run as root at 4:22 every day/etc/cron.All executable files in the daily directory, run-The parts parameter indicates that all executable files in the following directories are executed.

Example 2

# 21 per night:30 restart apache
3021***/usr/local/etc/rc.d/lighttpd restart
# 4 on the 1, 10, 22 of each month:45 restart apache
4541,10,22**/usr/local/etc/rc.d/lighttpd restart
# 1 every Saturday and Sunday:10 restart apache
101**6,0 /usr/local/etc/rc.d/lighttpd restart
# 18 per day:00 to 23:Restart apache every 30 minutes between 00
0,3018- 23***/usr/local/etc/rc.d/lighttpd restart
# 11 every Saturday:00 pm restart apache
023**6 /usr/local/etc/rc.d/lighttpd restart
# Restart apache every hour between 11 pm and 7 am
023- 7 /1***/usr/local/etc/rc.d/lighttpd restart
# Restart apache every hour
0* /1***/usr/local/etc/rc.d/lighttpd restart
# Restart apache on the 4th of every month and 11 o'clock every Monday to Wednesday
0114* mon-wed /usr/local/etc/rc.d/lighttpd restart
# Restart apache at 4 on January 1st
041 jan */usr/local/etc/rc.d/lighttpd restart
# Synchronize the time every half an hour
0 /30****/usr/sbin/ntpdate 210.72.145.44

note

It must be specified to be executed in the first few minutes of each hour, which means that the first * must be changed to a value.

Because * means every minute.

In addition, there is no difference between /1 and hourly, both are once every hour.

If it is set */2, it is actually the number of hours that can be divisible by 2 instead of the execution after 2 hours from the timing setting. For example, it will be executed after 9 o'clock to 10 o'clock.

Finally, you may encounter the following problem

Enter crontab -l under the root user to display

no crontab for root For example:

[ root@CentOS ~]# crontab -l

no crontab for root

This question is very simple, also enter crontab -e under the root user

Press Esc Press: wq Enter

There is no problem after typing crontab -l

The main reason is that this liunx server uses crontab for the first time, and the corresponding file has not been generated yet. This file is generated after editing (crontab -e)

The above method of Linux CentOS timing running script configuration is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Linux CentOS regularly run script configuration method
CentOS configuration swap exchange area method
CentOS8 yum/dnf configuration method of domestic sources
CentOS7.0 network configuration
CentOS 7.0 network configuration
CentOS7 basic configuration
Centos MySQL8 configuration
Linux firewall related configuration commands and operations (centos and ubuntu)
Compile and install Lnmp shell script under Linux centos
Linux (centos7) build gitlab
Xfs configuration on centos7
Centos kernel compilation configuration
Linux Centos7 install jdk1.8
CentOS (linux) install PostgreSQL
Mysql8.0.15 installation configuration (centos7)
Linux notes (1): CentOS-7 installation
A centos initialization script
Linux CentOS 7 installation tutorial
Centos 7 install JDK (Linux install jdk)