Linux Scheduled Task Management

In the actual production environment, there will always be some regular tasks, such as data backup, we can not always wait until that time to perform manually, then planning tasks will come into use.

One-time Planned Tasks

at

  • One-time Planned Tasks

at [HH:MM] [YYYY-MM-DD]
Set up, press Ctrl + D to exit

1. Get the current time of the system

[root@localhost ~]# date
2019 Wednesday, 28 August, 2000:30:41 CST

2. Setting up one-time planning tasks

[root@localhost ~]# at 00:33 2019-08-28
at> rpm -qa | wc -l > /opt/test.txt
at> <EOT>
job 1 at Wed Aug 28 00:33:00 2019
[root@localhost ~]# atq
1       Wed Aug 28 00:33:00 2019 a root

3. Verification results

[root@localhost ~]# cat /opt/test.txt
1318

atq

  • Query for planned tasks that have not yet been performed
[root@localhost ~]# atq
1       Wed Aug 28 00:33:00 2019 a root

atrm

  • Delete planned tasks that have not yet been performed
[root@localhost ~]# at 00:35 2019-08-28
at> shutdown now
at> <EOT>
job 2 at Wed Aug 28 00:35:00 2019
[root@localhost ~]# atq
2       Wed Aug 28 00:35:00 2019 a root
[root@localhost ~]# atrm 2
[root@localhost ~]# atq

Periodic Planning Tasks

crontab

  • Periodic Planning Tasks

- e: Editing the Scheduled Task List
- l: List shows scheduled tasks
- r: Delete the Scheduled Task List
- u: Designated user

  • Configuration file:
[root@localhost ~]# cat /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
parameter Explain
branch 0~59 integer
Time 0~23 integer
day 1~31 integer
month 1~12 integer
week Integers 0-7, 0 and 7 are Sundays
command Ordinary commands, program scripts
* Arbitrary time
- Continuous time range
, Discontinuous interval time
/ Specified interval frequency
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
50 1 * * * /usr/bin/cp -p /etc/passwd /root/passwd.bak
30 1 */3 * * /usr/bin/tar zcvf /opt/log.tar.gz /var/log/
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root

Keywords: Linux crontab RPM shell

Added by JasonGreen on Wed, 02 Oct 2019 22:52:43 +0300