-->
🏠 🔍
SHAREOLITE

Linux cronjobs practical examples

A cronjob is a linux scheduler utility which is part of “crond” linux service daemon used to schedule execution of  configured  tasks. There are two methods to configure the task & time of execution


  • Using the system cronjob file edit command with option –e i.e, “crontab –e
  • Using a flat file in which the list of tasks and time of execution are saved and passed as argument to the cronjob comand i.e “crontab <filename>

Syntax of a cronjob entry in a cronjob configuration file

<minute> <hour> <day of month> <month> <day of week>  <command to execute>

Field
Allowed Values
Minute
0 to 59
Hour
0 to 23
Day of Month
1 to 31
Month
1 to 12 or Names first 3 characters i.e, Jan , Feb .. Dec allowed
Day of Week
0 to 7   or Names first 3 characters i.e, Sun , Mon .. Sat allowed


Fields support value ranges , lists and step values as shown in below examples

Cronjob schedule value samples
Description
5 0 * * *
Run at 5th minute after midnight
15 14 1 * *
Run at 14:15 hrs on the 1st of every month
0 22 * * 1-5
Run at 10 PM on weekdays
5 4 * * sun
Run at 4.05 AM every Sunday
30 02 10 1,2,3 *
Run on Jan,Feb,Mar 10th at 02.30 AM
*/5 * * * *
Run every 5 minutes
0 * * * *
Run once a hour at the beginning of the hour
* * * * *
Run every minute
Verify the contents of cronjob file . Below file has 2 tasks configured

$ cat /opt/shareolite/cronjobs
*/2 * * * * sh Shareolite.sh 2>&1
00 02 * * * sh /opt/shareolite/2.sh >> /opt/shareolite.log 2>&1

Initialize cronjob file using the below command
$ crontab /opt/shareolite/cronjobs

Check the cronjob status by executing below command
$ crontab -l
*/2 * * * * sh Shareolite.sh 2>&1
00 02 * * * sh /opt/shareolite/2.sh >> /opt/shareolite.log 2>&1

Remove the cronjob by executing below command
$ crontab –r

For a linux user , only one cronjob can be initialised. Tasks requiring root user privileges are usually configured under root user. On OS reboot , tasks are reinitialised automatically and manual initialisation is not required.


–>