2020年9月8日星期二

Run schedule batch by crontab

ref: https://crontab.guru/every-1-minute

1. Show the existed crontabs.

$ crontab -l     

# do daily/weekly/monthly maintenance
# min   hour    day     month   weekday command
*/15    *       *       *       *       run-parts /etc/periodic/15min
0       *       *       *       *       run-parts /etc/periodic/hourly
0       2       *       *       *       run-parts /etc/periodic/daily
0       3       *       *       6       run-parts /etc/periodic/weekly
0       5       1       *       *       run-parts /etc/periodic/monthly

crontabs file is here:
$ls  /etc/crontabs 
root

$ cat root 
# do daily/weekly/monthly maintenance
# min   hour    day     month   weekday command
*/15    *       *       *       *     run-parts /etc/periodic/15min
0       *       *       *       *       run-parts /etc/periodic/hourly
0       2       *       *       *       run-parts /etc/periodic/daily
0       3       *       *       6       run-parts /etc/periodic/weekly
0       5       1       *       *       run-parts /etc/periodic/monthly
*       *       *        *      *       echo " run every minute."
#run-parts  shell-folder
on time trigger fired run the command.sh under the folder.
ex. */15    *       *       *       *     run-parts /etc/periodic/15min
every 15 minutes triggered to run shells under /etc/periodic/15min
$ run-parts 
BusyBox v1.31.1 () multi-call binary.

Usage: run-parts [-a ARG]... [-u UMASK] [--reverse] [--test] [--exit-on-error] DIRECTORY

Run a bunch of scripts in DIRECTORY

        -a ARG          Pass ARG as argument to scripts
        -u UMASK        Set UMASK before running scripts
        --reverse       Reverse execution order
        --test          Dry run
        --exit-on-error Exit if a script exits with non-zero
#Value Range:
min: 0-59, hour:0-23, day:1-31,month:1-12,weekday:0(sunday)-7
#***** run every one minute.

#can set values type:
list type:  min: 0,10,20,30 : run on min=00 or 10 or 20 or 30
range type: day: 1-5  : run on day=1,2,3,4,5 of every month
loop type:  min:*/10 : run every 10 minutes.  

Example:
15  *  *  *  * command  #every 00:15,01:15, 02:15,...23:15 will run
*/15 * * * * command   #every 00:15,00:30,00:45:01:00,...23:45 will run
30  6 *  *  * command   #every day 06:30 will run
30  0 1,2 * * command   #every 1st or 2nd of month,00:30 will run.
*/1     *       *       *       *       echo "Batch test start on:"`date "+%YYYY%m%d-%HH%MM%SS"` 2>>&1 | tee /var/log/cron.log
#check the result log exported from stdon and stderr.

2. Create a  sample batch shell.

$ vi 01.batch.sh
$ >echo "test batch :"`date "+%Y%m%d% %H%M%S"`  2>>&1 | tee /var/log/cron.log
$ >:wq
$ chmod 775 01.batch.sh
$ ls -la 
total 4
drwxr-xr-x    2 root     root            25 Sep  8 05:37 .
drwxr-xr-x    1 root     root            18 Sep  8 05:35 ..
-rwxrwxr-x    1 root     root            44 Sep  8 05:37 01.batch.sh
$ chmod o-x 01.batch.sh
$ ls -la 
total 4
drwxr-xr-x    2 root     root            25 Sep  8 05:37 .
drwxr-xr-x    1 root     root            18 Sep  8 05:35 ..
-rwxrwxr--    1 root     root            44 Sep  8 05:37 01.batch.sh

3. Edit crontabs by vi (*backup first!!!)
$ crontab -l > crontab-data
$ cp -p crontab-data crontab-data_`date "+%Y%m%d-%H%M%S"` 
$ vi crontab-data
$ crontab crontab-data 

4. Check by crond status 

$ crond status 
crond[343]: crond (busybox 1.31.1) started, log level 8

#if status is not started, start it by: crond on

没有评论: