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

MySQL extracts Json internal fields and dumps them as numbers

Table of contents background Problem Analysis 1. ...

Java uses Apache.POI to export HSSFWorkbook to Excel

Use HSSFWorkbook in Apache.POI to export to Excel...

TypeScript Enumeration Type

Table of contents 1. Overview 2. Digital Enumerat...

How to use a field in one table to update a field in another table in MySQL

1. Modify 1 column update student s, city c set s...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

Detailed explanation of how to exit Docker container without closing it

After entering the Docker container, if you exit ...

Introduction to common MySQL storage engines and parameter setting and tuning

MyISAM, a commonly used storage engine in MySQL c...

Should I use Bootstrap or jQuery Mobile for mobile web wap

Solving the problem Bootstrap is a CSS framework ...

Parsing Apache Avro Data in One Article

Abstract: This article will demonstrate how to se...

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...