Crontab realizes automatic scheduled restart of Linux server
For example: Set the method to restart at 5 am every day:
After successfully logging in to the server using the SSH tool, run the following command:
Press the Insert button to enter the edit mode.
Then type in the document:
0 5 * * * /sbin/reboot
(Note the middle space, 0 5 means restart every day at 4 am and so on.. 0 6 means 6 am..)
After editing, press esc to exit edit mode.
Type :wq to save and exit
Restart the crond service.
Service crond restart
The input content can be queried by the “crontab -l” command.
Cron is a timed execution tool in Linux that can execute commands without human intervention.
The meaning of the various parameters of the crontab command:
Each line in the crontab file created by the user represents a task. Each field of each line represents a setting. Its format is divided into six fields. The first five segments are time setting segments, and the sixth segment is The command segment to be executed, in the following format:
Minute hour day month week command
Minute: indicates the minute, which can be any integer from 0 to 59.
Hour: represents the hour, which can be any integer from 0 to 23.
Day: indicates the date, which can be any integer from 1 to 31.
Month: indicates the month, which can be any integer from 1 to 12.
Week: indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
Command: The command to be executed, which can be a system command or a script file that you have written yourself.
Linux centos server set automatic timing restart method
In the above fields, you can also use the following special characters:
Asterisk (*): Represents all possible values. For example, if the month field is 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 range of values with a comma-separated value, for example, “1, 2, 5, 7, 8, 9”
Middle bar (-): A range of integers can be represented by a middle bar between integers. For example, “2-6” means “2,3,4,5,6”
Positive slash (/): You can use a forward slash to specify the interval frequency of the time. For example, “0-23/2” means that it is executed every two hours. At the same time, the forward slash can be used together with the asterisk, for example */10, if used in the minute field, it means that it is executed every ten minutes.
Detailed crontab command
1. Command format:
Crontab [-u user] file
Crontab [-u user] [ -e | -l | -r ]
2. Command function:
With the crontab command, we can execute specified system directives or shell scripts at regular intervals. The unit of time interval can be any combination of minutes, hours, days, months, weeks, and above. This command is very useful for periodic log analysis or data backup.
3. Command parameters:
-u user: Used to set a user’s crontab service. For example, “-u lao8” means to set the crontab service for the lao8 user. This parameter is generally run by the root user.
File:file is the name of the command file, which means that file is used as the crontab task list file and loaded into crontab. If this file is not specified on the command line, the crontab command will accept commands typed on standard input (keyboard) and load them into the crontab.
-e: Edit the contents of a user’s crontab file. If you do not specify a user, it means editing the current user’s crontab file.
-l: Display the contents of a user’s crontab file. If no user is specified, the current user’s crontab file content is displayed.
-r: Delete a user’s crontab file from the /var/spool/cron directory. If no user is specified, the current user’s crontab file is deleted by default.
-i: A confirmation prompt is given when the user’s crontab file is deleted.
Other use cases of crond
Example 1: Execute command every 1 minute
* * * * * command
Example 2: Execution at 3 and 15 minutes per hour
3,15 * * * * command
Example 3: Execute at 3 and 15 minutes from 8 am to 11 am
3,15 8-11 * * * command
Example 4: Execute at 3 and 15 minutes from 8 am to 11 am every two days
3,15 8-11 */2 * * command
Example 5: Execution at 3 and 15 minutes every Monday from 8 am to 11 am
3,15 8-11 * * 1 command
Example 6: Restart smb at 21:30 every night
30 21 * * * /etc/init.d/smb restart
Example 7: 4:45 restarts smb on 1, 10, 22 every month
45 4 1,10,22 * * /etc/init.d/smb restart
Example 8: Restart smb every Saturday, Sunday 1:10
10 1 * * 6,0 /etc/init.d/smb restart
Example 9: Restart smb every 30 minutes between 18: 00 and 23: 00 every day
0,30 18-23 * * * /etc/init.d/smb restart
Example 10: Restart smb every Saturday at 11:00 pm
0 23 * * 6 /etc/init.d/smb restart
Example 11: Restart smb every hour
* */1 * * * /etc/init.d/smb restart
Example 12: Restart smb every hour between 11pm and 7am
* 23-7/1 * * * /etc/init.d/smb restart
Example 13: Restart smb at 11 o’clock every month and 11 o’clock every Monday to Wednesday
0 11 4 * mon-wed /etc/init.d/smb restart
Example 14: Restart smb at 4 o’clock on January 1st
0 4 1 jan * /etc/init.d/smb restart
Example 15: Execute scripts in the /etc/cron.hourly directory every hour
01 * * * * root run-parts /etc/cron.hourly