BIP KB:
How To Use Cron To Its Full Power

Article By alexander


img

How To Use Cron To Its Full Power

What is cron?

Cron the name of a program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. It is normally used for sys admin commands, like makewhatis, which builds a search database for the man -k command, or for running a backup script, but can also be used for just about anything.

A common use for it today is for your email program to connect to the internet and download your email on a regular basis.


cron - in a nutshell is a widely-known and useful tool (daemon) for periodically running tasks/commands. It's not that hard to understand the commands so as always, practice makes perfect. Lets get started.

With that said, let's get started understanding the full power from this simple and magical tool!


Installation

Some distro's like RedHat do not have it installed so lets install it.

To install it run:

yum install -y vixie-cron

and then start it:

service crond start

Use Autostart to make it start up automatically.


Paths

Cron has several different files in the system.

These are short descriptions of the files and the paths to the files:
- The global config file is located at /etc/crontab
- For Linux, the default path for users /var/spool/cron/
- For FreeBSD the default path for users /var/cron/
- File /etc/cron.deny - consists of a list of users who are disallowed to use crontab.

- Other directories which have scripts which are executable. There scripts names correspond to time.

/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
Note: Different OS's may have other paths!

Use

It's very simple to use. To setup a new cronjob you just need to edit the file, but remember it has its own format.

Here are some hints:

#minute (0-59),
#|      hour (0-23),
#|      |       day of the month (1-31),
#|      |       |       month of the year (1-12),
#|      |       |       |       day of the week (0-6 with 0=Sunday).
#|      |       |       |       |       commands
#|      |       |       |       |       |
or

# m h  dom mon dow   command

If you forget the format you can always use man crontab to get more help.

As always, you can simply edit the file and reload the daemon, but this isn't the right way, because you can make a mistake. The right way is use crontab.

To edit the cronjobs for a current user run:
crotab -e

To edit the cronjob for a different user use -u usename:
crotab -e -u alex


These commands will open a file in a editor by default. Usually it will open vi, but vi is not very user friendly. To set your favorite editor like nano or mcedit, just run this command before:

export EDITOR=mcedit

To run a script for example, command.sh at 14:15 in the first day of month, add this line and save the file:
15 14 1 * *     /full/path/to/commans.sh

If you make a mistake you will get a notice. Use the hint above!

To view cronjobs use flag -l. To view the cronjobs for other user use flag -u:

crontab -l -u hop

Scripts

If you are still having problems understanding the file format, don't worry. Try the crontab2english.pl script. (it may require some additional perl modules).

This script will help you to setup a new cronjob using English
Just run:

perl /usr/share/doc/cron/examples/crontab2english.pl

You can find other useful scripts in the doc folder /usr/share/doc/cron/examples/
Note: In other Linux distribution the path may be different.

Just explorer the document directory to find:

cron-stats.pl
crontab2english.pl
cron-tasks-review.sh

Notifications

When cron runs the tasks, it will notify the owner by email. So if your cronjob runs too often you will get a lot of emails from cron in your mailbox.

To disable these notifications you need to add this line to the end:

>/dev/null 2>&1

It will redirect all the output to /dev/null (blackhole).

The commands look like:
55 23 * * * /opt/scripts/mysql_backup.sh > /dev/null 2>&1

Another way is to set variable on the header file MAILTO="", but it affects all cronjobs in file.

Alias for Safety

Sometimes when you're in hurry it's easy to mix up letter like "e" and "r" because they are next to each other. It may have irreversible consequences:

# from man crontab
-r option causes the current crontab to be removed.


So if you type crontab -r and press Enter and lose all current cronjob.

To protect it, use crontab with flag -i

Note: Linux already may have the flag -i
# from man crontab
-i option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.

Check it alias | grep cron

[root@donotstop]# alias | grep cron
alias crontab='crontab -i'


If you don't have it - just add this line to /etc/bashrc:

alias crontab='crontab -i'

Lock

If your crontab runs too often, for example once a minute - than you may run into a situation where the running cronjob doesn't finished and a new one starts again.

To protect against this type of situation use - flock (FreeBSD version it's called - lockf).

Example:

/usr/bin/flock -nx lock_name /full/path/to/command >/dev/null 2>&1

Add cronjob from bash script

If you write bash scripts and need to setup a cronjob, then follow these steps:

1. coppy the current crontab to temp file

crontab -l > cur_cron

2. add the new cron job into the file
echo "00 12 * * 1-3 echo hello" >> cur_cron

3. install new file
crontab cur_cron

4. delete temp file
rm cur_cron

InCron

You need to also know about another cool thing - incron.

It's not quite the same as cron, but the idea is similar. It's a kernel daemon that watches for actions on the file system within inotify and then runs a command for a specified action.

To learn more about incron, do a simple search above for incron to get more information about this powerful command.




Tags: , , , , , , , , , ,

Spin up a VPS server in no time flat

Simple setup. Full root access. Straightforward pricing.

DEPLOY VPS SERVER

Leave a Reply



Feedbacks