[OS Configuration] Configure Linux background job (cron job)

Infrastructure/Linux Common|2019. 2. 28. 22:17

Hi This is SAP Ops ST03.

This time, we are going to schedule background job in linux - called cron job.


#1 Format & Example

* * * * * command ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ └───────── Day of week (0 - 6) (0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat)

│ │ │ └───────── Month (1 - 12) │ │ └───────── Date (1 - 31) │ └───────── Hour (0 - 23) └───────── Minute (0 - 59)

** From : 제타위키 - 리눅스 반복작업 cron, crond, crontab


#2 How to Configure

Check your command first. It will be create as shell script.

Will schedule it for every minutes at this time.

root@saptest01:# vi /home/user01/cron_test.sh


Append a command that you want to schedule on "cron_test.sh" file.

It is command to write current time on "conr_result.txt" file. 

echo "It is now" `date +%T` >> /home/user01/cron_result.txt


Assign additional authorization on script file.

root@saptest01:# chmod 755 /home/user01/cron_test.sh


Register command

root@saptest01:# crontab -e


Put below sentence on crontab.

* * * * * /home/st03/cron_test.sh


Now we can restart cron, background job scheduler.

root@saptest01:# systemctl restart cron


Now we can check whether this job scheduled properly.

root@saptest01:# crontab -l

# DO NOT EDIT THIS FILE - edit the master and reinstall.

# (/tmp/crontab.zAggNS installed on Fri Jan 18 04:56:59 2019)

# (Cronie version 4.2)

* * * * * /home/st03/cron_test.sh


Check the result of cron job. - Executing in every minutes.

root@saptest01:# cat /home/user01/cron_result.txt

It is now 12:26:01

It is now 12:27:01

It is now 12:28:01

It is now 12:29:01


#3 Appendix - Schedule in seconds

Basically, this cron job can be scheduled in every minutes. So to schedule in seconds, it can be mentioned in program or script.


Now we can check how to adjust the script to execute in seconds.

root@saptest01:# vi /home/user01/cron_test.sh


Please refer following to execute them in every 15 seconds.

(sleep 15 && echo "It is now" `date +%T` >> home/user01/cron_result.txt) &

(sleep 30 && echo "It is now" `date +%T` >> home/user01/cron_result.txt) &

(sleep 45 && echo "It is now" `date +%T` >> home/user01/cron_result.txt) &

(sleep 60 && echo "It is now" `date +%T` >> home/user01/cron_result.txt) &


Check the result of cron job.

root@saptest01:cat /home/user01/cron_result.txt

It is now 12:34:16

It is now 12:34:31

It is now 12:34:46

It is now 12:35:01

It is now 12:35:16

It is now 12:35:31