Not only do cronjobs encourage us to do more with the server – such as trimming logs or mechanising backup – they also equate to more time down the pub. 😛
… Already you should be totally ‘sold'. So read on.
Maybe you've used cron jobs in a shared hosting past life? To simplify this guide we'll compare using cron in a control panel like cPanel alongside creating the equivalent tasks using the command line. Remember, the panel option is simply a shiny GUI way to set up the exact same server-side cron process.
Set up Cron Jobs from cPanel
cPanel or not, the procedure for setting up a cron job in a panel is broadly the same.
The scheduler itself is pretty self-explanatory. In cPanel there's a Common Settings drop-down with scheduling presets so you could be lazy and use one of those.
Let's say you want a weekly disk usage report echoed to a file. Here's the ‘nix command.
And in the piccy at the top of the page you can see that command applied to cPanel's cron scheduler, together with a weekly preset that can be set uniquely, else via one of those Common Settings defaults.
Nice and simple. Not exactly fast, what with all the page loads, but brain-dead easy. Using the terminal to achieve the same thing, though, is hardly rocket science and, understood, saves time.
Set up Cron Jobs from the Terminal
Control panel or not, this is quicker and easier. For one thing, invariably you need to create some simple script or other that you want cron to run so, hey, you may as well create that and add the cron job at the same time. Edit the crontab file, where cron jobs are ordered.
To break down cron's syntax that we provide to set up a task, we have this.
It's dead simple to understand. It's merely five time options along with some task.
So no big deal there, then. Say you want a daily whatever. Do this.
Registered in the crontab file, that sets whatever to clock in each day at 22:15 hours.
- Security Tip 1: Check for Con Crons
- Security Tip 2: Restricting Cron Access
Clearly cron is a pretty powerful tool. A related task scheduling tool is at.
If you have a shared or multi-user environment you should restrict use of the at and cron tools to the root superuser and, maybe, to your human administrators.
To do that create two allow files, in this case using the nano text editor.
Within each, add the users to allow, one user per line. You should add root to each.
You must also create the converse deny files.
They can be blank. However, don't create blank deny files unless you've populated allow files because that gives global access.
- Security Tip 3: Hide Cron's Output
So far so good. But. When things are run on a server, they get logged. Maybe we're running a script that contains sensitive data, like a database password? You get the picture: not good.
So this …
We change to this …
Cron for Windows
Some of you less fortunate types, running Windoze locally, may be wondering how to use cron to automate various stuff on the local box. Well, sorry but you can't. There's always the Windows Task Scheduler which, admittedly, is pretty fair play. There's also cron's dozy cuzzie, cronw, so check that out.
- cronw – http://cronw.sourceforge.net
So there's a brief heads-up into this top administrative tool. You may be wondering how to use it to do something more useful than checking your disk space. Fair point …
Securing Off-Server Backups using Cron Jobs
Aah. Nice. In the next few days we'll have a spanky new tutorial about how properly to backup a database so that it's not sitting around your server in a text (.sql) file, nor being emailed in plain text, but correctly is being sent to you scrambled with the server having no trace of the relatively easily hackable data dump. Definitely check that out. I'll link it here. Ciao for now.