The cron daemon is a long-running process that executes commands at specific dates and times. You can use this to schedule activities, either as one-time events or as recurring tasks.
For commands that need to be executed repeatedly (e.g., hourly, daily, or weekly), use crontab, which has the following options:
crontab -a filename Install filename as your crontab file. On many systems, this command is executed simply as crontab filename (i.e., without the -a option).
crontab -e Edit your crontab file, or create one if it doesn't already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Displays the last time you edited your crontab file. (This option is only available on a few systems.)
crontab -u user Used in conjunction with other options, modify or view the crontab file of user. When available, this option can only be used
by administrators.
field allowed values for cron
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
There are 5 fields as shown
Each line in the /etc/crontab file has the format:
Quote:
minute hour day month dayofweek command
Have a Closer look
Code:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
By looking at this, it's easy enough for me to set up my cron for specific time that i want my cron to run.
example if i want my script to be execute at 1am on monday every week, i would have this in cron tab:
Code:
00 1 * * 1 ./path/to/myscript.sh
if i want the script to be executed every 15 minutes of a day i would have:
Code:
0/15 * * * * ./path/to/myscript.sh
The cron above is equal to:
Code:
0,15,30,45 * * * * ./path/to/myscript.sh
And so on, by looking at the table you can set up your cron to suit your need.
Now if you don't want all of the users in your system to use cronjob, you only only want yourself and user
Bob to be able to use cron job.
You can create 2 new files in
/etc/ directory
/etc/cron.deny and
/etc/cron.allowTo allow user root and
Bob to be able to cron:
Code:
#> echo Bob >>/etc/cron.allow
Code:
#> echo root >>/etc/cron.allow
and Deny the rest
Code:
#> echo ALL >>/etc/cron.deny
You can also manually edit the cron.allow by a text editor like
nano, pico, or vi to add in the users to allow or deny
Hope you find this useful.