Cron Automation in Linux & Unix Systems

logo-512x512-300x300

Prerequisites

Cron is commonplace on most Linux systems so any typical open source OS will work for this post. Any type of hosting suffices too as long as the system is a Linux one. You can buy yourself server hosting from companies such as Hoverdata, who are specialists in providing reliable server hosting through affordable plans.

Introduction

The classic tool for setting up regularly run automatic processes or tasks in Linux/Unix systems is named cron, and the tasks it automates are often informally referred to as cron-jobs.  The first advantage of using Cron to schedule processes is the fact that it comes as standard in the vast majority of Linux like distributions. Meaning you are more than likely to have it at your disposal, regardless of the Linux system type you’re using. In turn the documentation available for Cron is quite ubiquitous and will be the same for most scenarios.

Historically it’s main strength from a server background is to automate recurrent admin and maintenance style duties. Administrators can allocate the exact timing a job is run and how frequently along with several other custom criteria.

Continue on to see how cron is used to set up recurrent commands on a VPS which will help with regular admin activities.

Step 1 — Understanding Cron Syntax

To add a cron-job we need to understand how to tell “cron” when and what to run.

Fortunately once understood the syntax is quite easy to read and decipher.

An example cron-job looks like this:

Each initial asterisk represents a certain time value. From left to right the value goes:

  1. Minute
  2. Hour
  3. Day (Month)
  4. Month
  5. Day (Week)

So this first portion before the ping command dictates when and how often the task is run.

The task you want to run is simply the amended command that always comes after, anything that you can run from the command line normally can be used as the task (within reason).

In our above example I’ve provided at every week at eleven o’clock on Friday the ping command is run and targeted at www.google.co.uk


Step 2 — Adding A Cron-Job

Here’s how to input your command to cron once you have an idea of what you want it to be. I’ll use the exact same example from the previous step so it makes sense.

To add it we use crontab  which serves as the input file for cron.

Run the previous command and select a text editor from the list of choices.

Now add your previously created cron syntax as a new line at the bottom of the file, making sure not to include a # symbol at the start otherwise it will not be processed.

Cron-job Example

Ping Cronjob

 


Step 3 — Displaying Cron-Jobs

To check what jobs are scheduled to run in your crontab you can use the -l parameter with crontab:

You may want to remove the preamble comments that are generated upon first use of crontab from the contents, as this is displayed whenever you run the above command and is not vital information.


Step 4 – Different User’s Cron-Jobs

Whilst adding and running these jobs remember they are run under the user you added them to crontab as.

Potential problems that can arise are access and permissions issues for the command you re trying to run as a job, when the user does not have the ability to implement them.

if logged in as root the jobs you add will be for root and therefore have access to whatever you need. Also from root or a privileged user you can add jobs for other users with this command.

Replace “username” with the Linux user’s username you wish to add the job to.


Step 5 — Cron Security 

Perform this section as root if you choose to complete it.

In the event that you don’t want a certain user to be allowed to add jobs to their crontab, you can add them to an allow file.

If you plan to use this you must create an allow file in /etc/ in that the file is named cron.allow and has the specific users within it.

Here is one way of going about doing this where username is the user you want to have access to cron scheduling.

In the future simply add usernames to the file with a text editor or other means, separated by new lines after each other (as root again).


Conclusion 

Cron is invaluable to a wide variety of tasks that require constant implementation, and should not be overlooked by system admins when maintaining a server. Despite its age and relatively basic operation, the tool is still as useful as it was when first introduced. There are even more syntax commands you can make use of with cron if you want to learn them too.

We hope this has proved useful and gets you started in using the potential of cron.

Leave a Reply

Logged in as Scarlz. Log out?

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">