Archive

Posts Tagged ‘cron’

Linux Automation for Absolute Beginners, Part 2

December 12th, 2009 No comments

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!

Categories: linux Tags:

Linux Automation for Absolute Beginners

December 7th, 2009 No comments

Linux command-line scheduling is done via a program called cron. Cron is a system program that is running all the time and is similar to the Windows scheduler which allows you to run commands/programs at predefined times and intervals. To find out if it’s running on your system do the following:

$ ps aux | grep crond

root      2223  0.0  0.0  4508  840 ?        Ss   Mar09   0:00 crond

The output above shows that crond is running.

The commands or scripts that you want cron to run are defined in a file called crontab, and every user has their own independent crontab file. The crontab file has 7 specific fields. Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is layed out:

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), Task

Further definition of each field:

Minute = Minute of the hour, 00 to 59. * Will indicate every minute (details later)
Hour = Hour of the day in 24-hour format, 00 to 23. * Will indicate every hour (details later)
Day = Day of the month, 1 to 31. * Will indicate every day (details later)
Month = Month of the year, 1 to 12. * Will indicate every month (details later)
Weekday = Day of the week, 3 chars – sun, mon, tue, or numeric (0=sun, 1=mon etc)
Task
= The command you want to execute

NOTE: An asterisk (*) in a field can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used.

Some examples of a crontab entry:

01 02 * * * /usr/bin/program

– The above example will run /usr/bin/program at 2:01AM on every day of every month.

*/5 * * * * /usr/bin/program

– The */5 is known as a short form equivalent to 0,5,10,15,20 etc…basically executing ‘program’ every 5 minutes. Other examples are */2 for every 2 minutes, */30 every 30 minutes and so on.

00 23 * * * /usr/bin/program

– Run a task at 11PM every night.

15 02 * * 1-5 /usr/bin/program

– Run task at 2:15am every weekday morning

0 0 1,15 * * /usr/bin/program

– Run task the first and fifteenth of each month

0 0 * * 1 /usr/bin/program

– Run task every Monday.

NOTE: When entering commands into the crontab file, make sure that you use absolute pathnames (i.e /usr/bin/program.sh) to specify the script in crontab. This is because when cron executes your task, it does not have the same $PATH variable defined.

Crontab Options

  • The -l option causes the current crontab to be displayed on standard output.
  • The -r option causes the current crontab to be removed.
  • The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.
  • The -u option is used by root to change other user’s crontab.

After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically.

To create a crontab file, simply type ‘crontab -e’ at your command prompt. This’ll bring up crontab in your default editor. If you prefer a different editor, you can type:

EDITOR=vi && crontab -e

You can replace vi with your favorite editor 🙂

You can find more examples by typing: ‘man 5 crontab’ at your shell prompt. On second part of this article, I will include many tips and tricks one can do with the cron.

Categories: linux Tags: