How To Use Cron To Its Full Power

Article By: alexander


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!


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.


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.

Note: Different OS's may have other paths!


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
#|      |       |       |       |       |

# 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, at 14:15 in the first day of month, add this line and save the file:
15 14 1 * *     /full/path/to/

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


If you are still having problems understanding the file format, don't worry. Try the 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/

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:


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/ > /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

[[email protected]]# alias | grep cron
alias crontab='crontab -i'

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

alias crontab='crontab -i'


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).


/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


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: , , , , , , , , , ,

Welcome to the healthcare-only HIPAA - GDPR compliant cloud. Exclusively hosted on a HPC environment!

Learn more or start today by choosing your secure HIPAA - GDPR compliant server's Operating System bellow and pick the package that's best for you.

BIPmd makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine, thousand or more.

Windows VPS options and add ons
Linux VPS options and add ons


Looking for a custom solution?

Our technicians can provide you with the best custom-made solutionss on the market, no matter whether you're a small business or large enterprise.

Get in touch

Leave a Reply