Linux-07-scheduled tasks

Keywords: crontab Linux ssh network

In Linux, the timing task is executed by crond program. It is started by default after power on like SSH, network, rsyslog and sysstat. By default, crond checks every other minute to see if there are any tasks to perform. If you have special needs, such as executing once a second, you need to write daemons.

View the contents of scheduled tasks

[test@CentOS6 ~]$ ll /etc/| grep cron
-rw-------.  1 root root     541 Aug 24  2016 anacrontab
drwxr-xr-x.  2 root root    4096 Aug  7 01:27 cron.d
drwxr-xr-x.  2 root root    4096 Aug  7 01:28 cron.daily
-rw-------.  1 root root       0 Aug 24  2016 cron.deny
drwxr-xr-x.  2 root root    4096 Aug  7 01:27 cron.hourly
drwxr-xr-x.  2 root root    4096 Aug  7 01:28 cron.monthly
-rw-r--r--.  1 root root     457 Sep 27  2011 crontab
drwxr-xr-x.  2 root root    4096 Sep 27  2011 cron.weekly

View scheduled tasks

[root@CentOS6 ~]# crontab -l
#time sync by test at 2018-8-9
*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1
#del file by test at 2018-8-10
00 00 * * 6 /bin/sh /server/scripts/del.sh > dev/null 2>&1
[root@CentOS6 ~]# 

The first five columns represent minute, hour, day, month and week respectively.

Scheduled task file:

[root@CentOS6 ~]# ll /etc/cron.deny 
-rw-------. 1 root root 0 Aug 24  2016 /etc/cron.deny
[root@CentOS6 ~]# ll /var/spool/cron/
total 4
-rw------- 1 root root 182 Aug 10 19:10 root

The content modified through crontab -e is actually the / var/spool/root file

[root@CentOS6 ~]# cat /var/spool/cron/root 
#time sync by test at 2018-8-9
*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1
#del file by test at 2018-8-10
00 00 * * 6 /bin/sh /server/scripts/del.sh > dev/null 2>&1

Generally speaking, in the timing task configuration file, the user's timing task is divided into six sections, and the system's timing task is divided into seven sections

[root@CentOS6 ~]# more /etc/crontab 
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

In time (i.e. in the first five fields), * stands for every, - stands for range,, stands for split time period, / n n stands for number, and means every n unit time

Services since crontab lock

[test@CentOS6 ~]$ chkconfig --list crond
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Check whether the service process has started

[test@CentOS6 ~]$ ps -ef | grep cron
root      1160     1  0 09:54 ?        00:00:00 crond
test      1220  1191  0 09:59 pts/0    00:00:00 grep cron

You can restart the service if it is not started

[test@CentOS6 ~]$ sudo su - 
[root@CentOS6 ~]# /etc/init.d/crond restart
Stopping crond: [  OK  ]
Starting crond: [  OK  ]

Write an example of automatic time recording

[root@CentOS6 logs]# crontab -l
#time sync by test at 2018-8-9
*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1
#del file by test at 2018-8-10
00 00 * * 6 /bin/sh /server/scripts/del.sh > dev/null 2>&1
#write logs to somefile
* * * * * echo `date +\%F\ \%T` >> /root/logs/test.log

You can also write the command to the script, and call the script to execute the scheduled task

[root@CentOS6 logs]# more log.sh 
echo "`date +%F\ %T` test" >>/root/logs/test.log
[root@CentOS6 logs]# crontab -l
#time sync by test at 2018-8-9
*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1
#del file by test at 2018-8-10
00 00 * * 6 /bin/sh /server/scripts/del.sh > dev/null 2>&1
#write logs to somefile
* * * * * echo `date +\%F\ \%T` >> /root/logs/test.log
#test sh log
* * * * * /bin/sh /root/logs/log.sh >/dev/null 2>&1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Posted by rascle on Sun, 05 Jan 2020 09:36:49 -0800