Linux Automation for Absolute Beginners, Part 2
In my first article, I provided some basic understanding of how scheduling on Linux operating system works and also gave some examples of how to schedule jobs using cron at different date and times. If you haven’t read it, please read Linux Automation for Absolute Beginners first.
This article will provide some additional and more advanced understanding of cron and mostly give examples of many neat tricks you can do via crontab file. For example, you can write single line cron entries to gather system statistics and email you details without even writing a script in a separate file or even run some scripts upon system reboots.
Some More Basics
There are a couple of different ways to use cron. In the /etc directory you will probably find some sub directories called cron.hourly, cron.daily, cron.weekly and cron.monthly. If you place a script into one of those directories it will be run either hourly, daily, weekly or monthly, depending on the name of the directory.
If you want more flexibility than this, you can edit /etc/crontab file which is the main configuration file. On a default Linux distro, the crontab will look something like this:
# cat /etc/crontab SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin HOME=/ MAILTO=hugefatass007@gmail.com # run-parts 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
The first part sets the variables $SHELL, $PATH, $MAILTO and $HOME.
$SHELL is the ‘shell’ cron runs under. If unspecified, it will default to
the entry in the /etc/passwd file.
$PATH contains the directories which will be in the search path for cron script.
If you have a shell script in /opt/bin, then you should add that to the PATH variable.
$MAILTO is who gets mailed the output of each command. If no one is specified, then the output will be mailed to the owner of the process that produced the output.
$HOME is the home directory that is used for cron. If unspecified, it will default to the entry in the /etc/passwd file.
** You can also set the above variables if you decide to create your own crontab file and entries.
Controlling Access to cron
You can choose to restrict cron usage by the use of /etc/cron.allow and /etc/cron.deny files. To stop a user using cron, just put their name in cron.deny, to allow a user put their name in the cron.allow. If you wanted to prevent all users from using cron, you could add the line ALL to the cron.deny file:
# echo ALL >> /etc/cron.deny
If you want to deny only user MrBabyMan to stop using cron to automate so many of his digg posts, then you can add a line MrBabyMan to the cron.deny file:
# echo madiga >>/etc/cron.deny
Location of crontab files
The individual crontab file is stored as text file in /var/spool/cron/crontabs but maybe different into your distro. However, normally they’re all under /var/spool/cron base.
Examples, Tips & Tricks
We’re done with providing basic information. Now to the actual part of the article that’ll give you many different example usage of crontab. You’ll start feeling comfortable using cron in no time.
– Run backup.sh at 11:59 Monday, Tuesday, Wednesday, Thursday and Friday:
30 11 * * 1-5 /usr/local/bin/backup.sh
– Run every minute:
* * * * * command
– Run every 10 minutes:
*/10 * * * * command
– Every weekday, touch file, the easy way:
0 6 * * mon-fri touch ~/file
– One last example, every hour between 6-10 on the hour and 4:
0 6-10,4 * * * touch ~/file
– Backup your crontab every morning at 6AM:
0 6 * * * /bin/crontab -l > /root/cronjobs.txt 2>/dev/null
– Run your command at :05 and :45 past the hour, every hour, Monday through Friday:
5,35 * * * 1-5 command
– Remove all of the files in you /tmp directory every morning at 4:45 AM:
45 4 * * * rm /tmp/*
– Run command at 7:00am each weekday [mon-fri]:
00 07 * * 1-5 command
– Run command on 1st of each month, at 5:30pm:
30 6 1 * * command
– Run command at 8:00am,10:00am,12:00pm and 2:00pm every day:
00 8,10,12,14 * * * command
– Run command every 5 minutes during market hours:
*/5 6-13 * * mon-fri command
– Run command every 3-hours:
0 7-23/3 * * * command
If you’re wondering why you keep getting an email each time your Cron job runs, it’s because crontab notifies you that it has completed (or not) the job you requested it to run. After everything is running smoothly, you may want to disable this feature, or redirect the output to a log file instaed of an email.
To disable, simply place >/dev/null 2>&1 after your command.
– This’ll write the email to no where and you’ll no longer be receiving annoying cron emails:
45 4 * * * rm /tmp/* >/dev/null 2>&1
– This writes the email no where but keeps the output of the command in rmtmp.log file:
45 4 * * * rm /tmp/* > /home/MrBabyMan/cronlogs/rmtmp.log >/dev/null 2>&1
Note: One > means replace the current log with a new one, while (two) >> means append the current output to the end of the current log.
– The following will zip your access-log file into access-log.mmddyy.zip format:
30 0 * * * /usr/bin/zip -q access-log.`/bin/date +\%m\%d\%y`.zip access-log
– Run disk usage command and email result:
0 2 * * * du -h --max-depth=1 / | mail -s "Disk Usage" mrfatass@mynitor.com
– Send list of users logged on to server via email:
59 11 * * * last | grep date '+%a %b %d' > /tmp/users.out ; mail -s "Users Logged Today" hugefatass007@mynitor.com
Note: You can take this idea and email out many different types of alerts to your email at anytime.
Special Entries
There are several special entries, some which are just shortcuts, that you can use instead of specifying the full cron entry. The most useful of these is probably @reboot which allows you to run a command each time the computer gets rebooted.
You can alert yourself when server is back online after a reboot. Also becomes useful if you want to run certain services or commands at system startup.
The complete list of special entries are:
Entry Description Equivalent To @reboot Run once, at startup. None @yearly Run once a year 0 0 1 1 * @annually (same as @yearly) 0 0 1 1 * @monthly Run once a month 0 0 1 * * @weekly Run once a week 0 0 * * 0 @daily Run once a day 0 0 * * * @midnight (same as @daily) 0 0 * * * @hourly Run once an hour 0 * * * *
Add to the list!