Skip to content

crontab is your friend

February 5, 2014

If you having stuff to run, a scheduler software can help but for pure administrative tasks linked to your server (a Unix-like box, I mean Linux), using the crontab command is so cute…

There are some drawbacks: the command is hard to schedule (when you start to use it) and you in a lack of environment stuff: usually, you just access to /bin and you don’t have the user environment with some useful variables (like HOME, LANG, etc). But a so convenient command is a swiss knife to ensure the health of your server:

  • backups
  • free disk space
  • batches (like running a SQL stored procedure)
  • creating static web pages (it is a very simple way to generate an index.html file as a replacement on the index.php generated one which takes too much time).
  • etc. (add your usage in the comments)

I will not explain how to create a basic crontab line. Using crontab -e starts a basic editor add the crontab file contains the comments needed to add an entry. I will just give 2 advices:

Don’t generate output

All the output of a crontab generates an e-mail containing this output. Some administrators run the command in the crontab and receive an e-mail when the job is done because of the output. It is not a requirement: it is counterproductive. Why? Because when you receive the same message every day, you just delete it. In addition, if the message is too long, you will not see the errors inside.

It is better to remove the standard output (stdout) and just to keep the error output (stderr). When you do that, you receive a message only when something wrong. You can rely on the crontab daemon: I never see this daemon killed by someone or inactive. If you have added a command, the command will be executed.

Of course, it can be interesting to see all the disk usage of all of your disks in a daily e-mail (even in the case the remaining size is sufficient). In this case, I will advice to run a unique crontab that will group all the information you want to monitor (to get a single e-mail).

How to debug your command?

Usually, when you test the command you add in the cron, everything is fine. But when you start to use, you have an error. Basically, because of the environment variables are not set.

When the crontab failed, you get an e-mail from the crontab. If, like me, the e-mail is forwarded directly to your favorite mail program, you will have to ask for the “source” code of the e-mail (including the headers). Near every mail software can do that.

See the e-mail I received:

Return-Path: <>
X-Original-To: root
Received: by (Postfix, from userid 0)
id E3FF723894; Wed,  5 Feb 2014 22:40:02 +0100 (CET)
From: (Cron Daemon)
Subject: Cron <root@xxxx> /home/mongodb/mongobackup
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <>
Date: Wed,  5 Feb 2014 22:40:02 +0100 (CET)

/home/mongodb/mongobackup: 16: /home/mongodb/mongobackup: /root/bin/ not found


See the X-Cron-Env: you get ALL the environment variables in the headers of the mail: so useful. This is useful for debugging your script.





From → Computers

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: