crontab for Linux (scheduled tasks)

Posted by turboprop on Sun, 19 Dec 2021 09:56:22 +0100

effect

  • A program needs to be executed every minute to check the operation status of the system
  • The business data of the past day shall be counted every morning
  • Log files need to be backed up every week (if there is too much log information, the disk space will be occupied, and most of the logs are analyzed in real time within a week, and we will back up the logs beyond a week) at two or three o'clock in the morning every day
  • The database needs to be backed up every month

Common commands for single task

  • at define scheduled tasks
  • atq view scheduled tasks
  • atrm delete scheduled task

Common commands for periodic scheduled tasks

#crontab -l displays my scheduled tasks

#crontab -r clear all my scheduled task lists

Format description of periodic plan task file

          •           branch     Time    day    month    Weeks (five months)*(function of number)
                      
                            0-59  0-23 1-31 1-12 0-6
            

Minute: represents the minute, which can be any integer from 0 to 59*/ one
Hour: represents the hour, which can be any integer from 0 to 23.
day: represents the date and can be any integer from 1 to 31.
Month: represents the month, which can be any integer from 1 to 12.
Week: represents the day of the week. It can be any integer from 0 to 7. Here, 0 or 7 represents Sunday.
Command: the command to be executed can be a system command or a script file written by yourself.

In each of the above fields, you can also use the following special characters:
Asterisk (): represents all possible values, such as the month field. If it is an asterisk, it means that the command is executed every month after 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 (-): the middle bar between integers can be used to represent an integer range, for example, "2-6" means "2,3,4,5,6"
Forward slash (/): forward slash can be used to specify the interval frequency of time. For example, "/ 1" means to execute every two hours. At the same time, forward slash can be used with asterisk, for example * / 10. If used in the minute field, it means to execute every ten minutes.

Configure a single scheduled task

Broadcast every two minutes

[root@redhat ~]# at now + 2 minutes
warning: commands will be executed using /bin/sh
at> wall nihao!
at> <EOT>  //Press ctrl+d to exit
job 1 at Mon Aug 30 22:27:00 2021
                                                                               
Broadcast message from root@redhat (somewhere) (Mon Aug 30 22:27:43 2021):     
                                                                               
nihao!  //Execute only once

Configure recurring scheduled tasks

[root@redhat ~]# crontab -e

* * * * * wall Periodic task   //Broadcast every minute


If it is deleted, crontab -e can enter the deletion information again

Add script files based on single user

Add a script file
The suffix of script file is generally sh
if determines whether the user exists

[root@redhat ~]# vim a.sh

id root
if [$? -eq 0 ];then
        echo "root User presence"
else
        echo "root user does not exist"
fi

Execute script file

[root@redhat ~]# chmod a+x a.sh
[root@redhat ~]# ./ a.sh / / execute from the current directory
uid=0(root) gid=0(root) groups=0(root)
./a.sh: line 2: [0: command not found
root user does not exist

Add task information based on system level

Modify profile
[root@redhat ~]# vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

#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
  *  *  *  *  * redhat /var/spool/cron/a.sh

Restart service

[root@redhat ~]# systemctl restart crond

crontab -e is customized by the current user and is only effective for the current user, / etc/crontab is effective for all users of the system

Extended scheduled task

Suppose that when your planned task should be executed at a certain point in time, but it is not executed due to some factors (power failure), will the planned task be executed or not? When will it be executed?
After startup, it will check which scheduled tasks are executed and which are not executed, and then execute them within the specified time

#vim /etc/anacrontab
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
(check the cron.daily script 5 minutes after startup every day)
Every month

Topics: Linux