Task Schedule crontab

Keywords: crontab shell inotify

crontab command

-u: Specify a user, the current user without the -u option
-e: Develop planned tasks
-l: List scheduled tasks
-r: Delete scheduled tasks

View the configuration file for crontab:

[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

Left to right in profile
First: Minutes.1-59
Second place: hours.0-23
Number three: days.1-31
Fourth place: month.1-12
Number five: Weeks.0-6; sunday = 0 or 7
Sixth: Users
Seventh: Specific orders

Formats 1-5 can be used to represent a range 1-5;
Formats 1,2,3 can be used to represent 1 or 2 or 3;
The format */2 can be used to represent numbers that can be divided by 2, such as hours, every two hours

Example:

Run every 8 hours
0 /8 * *

1:00, 12:00, 18:00 a day
0 1,12,18 * *

Run from 9:00 to 18:00 a day
0 9-18 * * *

Define a task plan:

[root@localhost ~]# crontab -e
#Run every two days at 3 a.m.
0 3 */2 * *  /bin/bash /usr/local/sbin/bakup.sh >>/tmp/bakup.log 2>>/tmp/bakup.log
#Start crontab
[root@localhost ~]# systemctl start crond
#View crontab status
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since one 2017-12-04 22:42:18 CST; 36min ago
 Main PID: 696 (crond)
   CGroup: /system.slice/crond.service
           └─696 /usr/sbin/crond -n

12month 04 22:42:18 localhost.localdomain systemd[1]: Started Command Scheduler.
12month 04 22:42:18 localhost.localdomain systemd[1]: Starting Command Scheduler...
12month 04 22:42:18 localhost.localdomain crond[696]: (CRON) INFO (RANDOM_DELAY will be scaled ....)
12month 04 22:42:19 localhost.localdomain crond[696]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.

Matters needing attention:

  • When using crontab, write the absolute path to the command.Otherwise, incorrect execution may occur.Because it is very likely that the command to be executed is not in the PATH of the crontab configuration file.
  • It's best to add a log to each task schedule you write.Write both the correct output and the wrong input.This ensures that the task is well documented.
  • Task schedules for the corresponding users are available under directory/var/spool/cron/You can copy the entire directory for backup.

Posted by frikikip on Thu, 16 Jul 2020 07:27:41 -0700