Cron Best Practices

crystal_clear_app_terminal

A fast one for everyone.

If you have occasion to use POSIX based systems like Linux, UNIX, and yes, even Macintosh, then you will end up using cron eventually to schedule things to be done on your behalf. Cron is the tool built into these operating systems, and can seem intimidating to a new user/admin. Here are two quick tips to making cron immediately useful.

1. Generate a cron template.

2. Generate a local copy of your cron jobs.

Create a file in your home directory called cronjobs. Copy and paste the below template, and use it when needed to make a cronjobs file for another user:

____________________________________________________

In this file, you can see that I’ve got a job that copies a directory every hour, on the hour (the “0” means “minute 0“.

Now that you’ve saved this file as cronjobs, you can edit it to your heart’s content. When you are ready to implement the changes, simply do (in your home directory, where the file is located):

crontab cronjobs

That’s it! Now your jobs won’t get accidentally overwritten-a real pain after you’ve spent hours making it perfect. Espacially as root, it is very easy to whack someone’s existing cron jobs. This applies to jobs for system users as well. In that case, simply keep a separate cronjobs file for each system user, like:

cronjobs_bin, cronjobs_jboss, cronjobs_mysql, etc.

As root you can then implement each as:

crontab -u <user> cronjobs_<user>

Very easy, and you never have to worry about your crontab file, which is essentially a backup, being deleted. Now just in case someone is adjusting a user’s cron jobs another way, be sure to first check a suspect user’s jobs with:

The great thing about this command is that it will show your entire cronjobs file-including comment lines! In other words, it wil llook exactly as it does above (with your actual jobs, of course). This way, it is really easy to tell if it has changed. You can even use ful commands like diff.

Obviously, cron is a complex topic, and I natually suggest doing the obligatory man cron and man crontab to see the full functionality, but these man pages don’t tell you how to protect your system.

Leave a Reply

Your email address will not be published. Required fields are marked *