CRON 101: How to Create & Run Cron Jobs from a Terminal: vpsBible




new password?
login
X
using cron with cPanel image

Cron is a time-based scheduling utility that automates tasks. We like that! Not only does it encourage us to do more with the server, for instance to rotate logs or mechanise backup, cron jobs mean more time down the pub. Here’s how to set it up.

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.

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

  .. from blank box to cute-as server ..  

with this easy-to-follow copy/paste guide.

22+ parts with video, here’s the index.

Set up Cron Jobs from cPanel

cPanel or not, the procedure for setting up a cron job in a panel is broadly the same.

Click on the icon for something like Cron Jobs in your CP and you arrive at a page where you can Add New Cron Job, edit existing jobs and receive an email when jobs are done.

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.

du -h --max-depth=1 ~ > tmp/du

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.

crontab -e

If this is the first cron job you’re running the crontab file will be created. If you already added the above cron job example you’d see it listed in cron’s file.

cron crontab image

To break down cron’s syntax that we provide to set up a task, we have this.

* * * * * doSomething

It’s dead simple to understand. It’s merely five time options along with some task.

minute(s) hour(s) day(s) month(s) weekday(s) <em>command(s)</em>

So no big deal there, then. Say you want a daily whatever. Do this.

15 22 * * * whatever

Registered in the crontab file, that sets whatever to clock in each day at 22:15 hours.

Security Tip 1: Check for Con Crons

Given the chance, hackers will use cron for dastardly deeds. We need to check for unusual jobs in the crontab file.

If there are any cron jobs you didn’t set up, remove them or #comment them out while researching what they do.

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.

sudo nano /etc/cron.allow
sudo nano /etc/at.allow
state-of-the-art server rack image

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.

sudo nano /etc/cron.deny
sudo nano /etc/at.deny

They can be blank. However, don’t create blank deny files unless you’ve populated allow files because that gives global access.

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.

Cron-tastic

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.

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

with this easy-to-follow copy/paste guide.

“My local PC runs Windows” Show me for Linux/Mac

22+ parts with video, here’s the index ..

Manage Unmanaged VPS: Ubuntu-Nginx Administration

Already set it up? We’d best maintain it then.

Toggle to the ..  Ubuntu-Nginx Admin Index




Got a Question? Want to Comment?

This site has no comments system. We've got a better system!

Please use the link at the top of the post and goto this topic's forum thread:-

  • to help build community
  • for a superior knowledge base
  • to keep info in one place, not diluted between forum & comments.

For those posts ported from Guvnr.com (pre-Feb/2010), archive comments remain.

Handy info from these comments is in the process of being ported to the forum and attributed to the original author.

If you think this idea sucks, let me know.

Thank you.