Cron

From DISI
Revision as of 17:55, 30 June 2016 by Benrwong (talk | contribs) (Created page based on "ALL ABOUT CRON" from lab manual)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
vim /etc/crontab

In this file you should see this:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/ 
# For details see man 4 crontabs
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed

In /etc, there are five directories associated with cron: cron.d, cron.daily, cron.hourly, cron.monthly and cron.weekly

To enable the scripts in these directories to be run you need to add them to the crontab, so, /etc/crontab should look like this:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/ 
# For details see man 4 crontabs
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
What this means is that: cron.hourly will run every hour on the first minute. cron.daily will run at 4:02 am everyday cron.weekly will run every week at 4:22 am on Sunday cron.monthly will run on the first of every month at 4:42 am

I added a script to clean out the /tmp directory every night like this: In /etc/cron.daily/ I created a file called clean_tmp. I added the lines:

#!/bin/bash 
rm -rf /tmp/*

Saved and quit. Then I typed chmod a+x clean_tmp That’s it!