i Linux basic -part 2 – All things in moderation

Linux basic -part 2

4.Linux Scheduler ( Cron Job)
  • Cron is a utility that helps us create schedule to perform a certain task/command. As we know that /etc having configuration files for most of services same as for cron.
  • We will jush go through a quick review of how dose it work and how do we set it up. The follwing is the hierarchy for it.
First * represent min 0-59
Second * represent hour 0-23
Third * represent day of month 1-31
Forth * represent month 1-12
Fifth * represent day of week 0-6
* How to use crontab

Let’s say you have a script which backs up important files, or creates a report about system statistics, for example. Let’s say the script is called /home/myname/scripts/do-every-day.sh, and you want to run it every morning at 7 A.M.
To edit the crontab, use this command:

crontab -e  
This will open the crontab in a text editor (Usually this is **vi** or **vim**, but it may be something else depending on your **Linux distribution**).  

The default crontab file looks like this:

These lines all start with a # because they are comments; they are ignored by cron, and are just there for you to read.
So, now let’s add our job to the crontab. Each job you add should take up a single line.
We want our job to run at 7 A.M., which would be minute 0, hour 5, every day of the month, every month, every day of the week. We need to add a line to the bottom of the file which looks like this:

0 7 * * * /home/myname/scripts/do-every-day.sh  

The asterisks (“*”) in our entry tell cron that for that unit of time, the job should be run “every”. You can now save the file and exit the text editor. In vi, this is done by pressing ESCAPE and then typing :wq (for “write and quit”) and pressing ENTER. crontab will give you the following message:

crontab: installing new crontab  

…and return you to the command line. Your script will now run automatically at 7 A.M., every day.
To view your crontab, you can use this command:

crontab -v  

…or, to remove your crontab so that there no jobs are ever executed by cron, use this command:

crobtab -r  
More examples
15 6 2 1 * /home/melissa/backup.sh

Run the shell script /home/melissa/backup.sh on January 2 at 6:15 A.M.

15 06 02 Jan * /home/melissa/backup.sh

Same as the above entry. Zeroes can be added at the beginning of a number for legibility, without changing their value.

0 9-18 * * * /home/carl/hourly-archive.sh

Run /home/carl/hourly-archive.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.

0 9,18 * * Mon /home/wendy/script.sh

Run /home/wendy/script.sh every Monday, at 9 A.M. and 6 P.M.

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup

Run /usr/local/bin/backup at 10:30 P.M., every weekday.

* Cron Permission

Two files play important role in cron.
Cron Permission
Two files play important role in cron.


If these files exist, then they impose some restriction accordingly on users. That is, if a user is in deny list, so he/she won’t be able to schedule any job/task and if user is in allowed list then she/he will be able to add schedule job/task. All we have to do is just add user name in either of these two files
Cron Files

/etc/crontab: system-wide crontab
[email protected]:~#cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don’t have to run the 'crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.monthly )


Leave a Reply