How to configure Linux CentOS to run scripts regularly

How to configure Linux CentOS to run scripts regularly

Many times we want the server to run a script regularly to trigger an operation. For example, when uploading using Qiniu's tool, if a new file is added to the synchronization file, we can provide a scheduled script to complete the synchronization command we need (Qiniu's qrsbox tool will synchronize automatically. You only need to add a file to the synchronization folder to automatically monitor the upload).

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 that installs, uninstalls, or lists the tables used to drive the cron daemon.

2. Enable crontab service

service crond start //Start the service

Use the following method to start and stop this cron service:

service crond start //Start the service

service crond stop //Shut down the service

service crond restart //Restart service

service crond reload //Reload configuration

View the 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 time by executing the command: ntsysv

Add to auto-startup:

chkconfig –level 35 crond on

In addition, let me introduce the ntsysv and chkconfig commands:

The ntsysv command is a graphical interface management mode to set the boot startup. It needs to be installed before it can be used. After yum install -y ntsysv is installed, you only need to run ntsysv to display a graphical management interface.

Up and down keys: can be used to move between services in the middle box;

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

Tab key: can move between boxes, OK, and Cancel;

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

Regarding the chkconfig command line format, set whether to start automatically at boot or query the running status of a service at 6 boot levels.

Set the crond service to start automatically at boot:

[root@CentOS ~]# chkconfig crond on

View the running status of crond services at each boot level

[root@CentOS ~]# chkconfig –list crond

crond 0: Disable 1: Disable 2: Enable 3: Enable 4: Enable 5: Enable 6: Disable

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

Cancel the automatic startup of crond service:

[root@CentOS ~]# chkconfig crond off

3. Set the script to be executed

There are two ways to add a new scheduling task:

1) Enter crontab -e in the command line, add the corresponding tasks, save and exit.

2) Edit the /etc/crontab file directly, that is, vi /etc/crontab, and add the corresponding tasks.

The crontab -e configuration is for a certain user, while editing /etc/crontab is for system tasks.

View scheduled tasks

crontab -l //List all current scheduled tasks

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

Delete a task scheduler

crontab -r //Delete all task scheduling jobs

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 explanation is quite intuitive.

Asterisk (*): represents all possible values. For example, if the month field contains an asterisk, it means that the command operation is executed every month after the constraints of other fields are met.

Comma (,): You can specify a list range of values ​​separated by commas, for example, "1,2,5,7,8,9"

Middle bar (-): You can use the middle bar between integers to represent a range of integers, for example, "2-6" means "2,3,4,5,6"

Forward slash (/): You can use a forward slash to specify the frequency of the time interval, for example, "0-23/2" means execution every two hours. At the same time, forward slashes can be used together with asterisks, for example, */10, if used in the minute field, means execution every ten minutes.

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

Example 1

: : : : : : : : : : : : : : :
# Execute all executable files in the /etc/cron.daily directory as root at 4:22 every day. The run-parts parameter indicates that all executable files in the following directory will be executed.

Example 2

#Restart apache at 21:30 every night
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#Restart apache at 4:45 on the 1st, 10th, and 22nd of every month
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#Restart apache at 1:10 every Saturday and Sunday
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
#Restart apache every 30 minutes between 18:00 and 23:00 every day
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
#Restart apache every Saturday at 11:00 pm
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
#Restart apache every hour between 11pm and 7am
0 23-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 every Monday to Wednesday at 11 o'clock
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#Restart apache at 4:00 on January 1st
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
#Synchronize time every half hour 0/30 * * * * /usr/sbin/ntpdate 210.72.145.44

Notice

* *1 * * * The command means it will be executed every minute within every hour.

You must specify the number of minutes of each hour to execute, that is, the first * must be changed to a number.

Because the * represents every minute.

There is no difference between /1 and hour, both are once every hour.

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

Finally, you may encounter the following problem

Enter crontab -l as root user to display

no crontab for root For example:

[root@CentOS ~]# crontab -l

no crontab for root

This problem is very simple. Also enter crontab -e as the root user.

Press Esc Press: wq Enter

There is no problem when you enter crontab -l

The main reason is that this is the first time that this liunx server uses crontab, and the corresponding file has not been generated yet. After executing the edit (crontab -e), this file is generated.

The above method of configuring scheduled scripts in Linux CentOS is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to set up a scheduled task to execute a specified script in centos
  • CentOS7 Setting Scheduled Tasks
  • How to set scheduled restart using crontab in Linux CentOS
  • Linux crontab scheduled task configuration method (detailed explanation)

<<:  Implementation of built-in modules and custom modules in Node.js

>>:  Detailed explanation of the functions and usage of MySQL common storage engines

Recommend

How to implement a simple HTML video player

This article introduces the method of implementin...

Docker solves the problem that the terminal cannot input Chinese

Preface: One day, I built a MySQL service in Dock...

How to configure Linux CentOS to run scripts regularly

Many times we want the server to run a script reg...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

Detailed explanation of Linux netfilter/iptables knowledge points

Netfilter Netfilter is a packet processing module...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which mea...

The easiest way to make a program run automatically at startup in Linux

I collected a lot of them, but all ended in failu...

HTML+CSS to achieve charging water drop fusion special effects code

Table of contents Preface: accomplish: Summarize:...

Detailed example of using case statement in MySQL stored procedure

This article uses an example to illustrate the us...

Vue uses ECharts to implement line charts and pie charts

When developing a backend management project, it ...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

Detailed explanation of Alibaba Cloud security rule configuration

Two days ago, I took advantage of the Double 11 s...