Tag Archives: scheduled task

How to Set an Hourly Cron in Ubuntu Server Tips, Tricks and Tutorials 15 APR 2015

Just a quick reminder to myself as to how to set an hourly cron on an Ubuntu Server instance using the standard Crontab mechanism (use crontab -e to edit your crontab).

hourly - clock turning 12

Now “* */1 * * * command.sh” (similar to how you would do one for every couple of minutes) may look very tempting at first glance, but this is in fact wrong. While what you are doing here does indeed say to run every hour (*/1), the first * means that it needs to run every minute. So what you are basically doing is runing the command at the start of each minute for each and every hour.

Not exactly what we’re looking for.

Instead, the correct way would be to simply specify the minute to run at – so say 0 to indicate the top of the hour. So a correct string to achieve a scheduled task that runs once an hour would be:

0 */1 * * * command.sh

A small, but significant point to remember then!

(Also, useful to remember that if you want to run your command with administrator privileges, you need to edit your crontab using sudo crontab -e. This crontab is separate for one edited without the sudo command!)

Related Link: https://help.ubuntu.com/community/CronHowto

How to test if a Crontab is working in Ubuntu CodeUnit 15 JUN 2012

Editing your cron jobs with the crontab -e command will ensure that your crontab is at least syntactically correct, by not installing it unless it is a valid cron file.

Outside of that though, it falls to you to test that your jobs are in fact running correctly. This can be achieved by piping the cron command output to a file that you control for each one of your jobs.

In practice:

00 01 * * * bash mycommand1.sh > /tmp/mycronjob.log 
00 02 * * * bash mycommand2.sh > /tmp/mycronjob.log

And in the same vein, you can check that the crontab as a whole is being executed by creating a job right at the end of the cron list that runs every minute and pipes out its output to a file that you control:

*/1 * * * * echo "Success! $(date)" >> /tmp/cronwatch.log

Note that > means rewrite the target file every time, whilst >> means append to the end of the file (and create if not exist).

Nifty

Related Link: https://help.ubuntu.com/community/CronHowto

Ubuntu Cron: How to Schedule Your PC to Automatically Shutdown Every Evening CodeUnit 10 AUG 2011

Electricity is expensive at the moment and as such every little bit that you save helps control this cost just that little bit more. However, I am guilty of often just leaving devices on for no apparent reason, and so this next little trick goes a long way in making sure I can’t get away with leaving my study a blur of various color LED lights all night long.

As with Window’s scheduled task functionality, Ubuntu comes with its Crontab, a service that is able to run scheduled cron jobs that you can assign per user. We’ll leverage this ability to make sure that our PC powers down just after midnight, just in case we forgot to hit the power button before turning in for the night.

Because the native shutdown command required elevated sudo rights, open the your crontab with the following command via the terminal:

sudo crontab -e

Note that if you don’t have a crontab set up yet, it will prompt you to select an editor. And yes, they are right. Nano is the simplest editor to use in this instance.

On a new line, enter:

01 00 * * * /sbin/shutdown -P now

Save and exit. The machine will automatically install the updated cron.

What the above command does is say that at 1 minute past midnight, execute the shutdown command (which is stored in /sbin) using the -P flag to indicate power down and now to say this must happen straight away.

Nifty.

Ubuntu: How to Add a Cron Job CodeUnit 10 JUN 2011

Scheduled tasks are known as cron jobs in Linux (though you could of course make use of the Gnome Scheduled Tasks tool if you would prefer) and are controlled by what are known as crontabs, basically simple text files containing a list of commands to be run at a specified time or interval. To add your own cron job is actually pretty simple.

The command:

crontab -e

…opens the crontab for the current user for editing purposes. Note that sudo crontab -e loads a different crontab from the one without sudo, one with more elevated rights of course.

Once the editor is open, you can specify a new cron job by adding a new line in the following format.

Crontabs are laid out in six distinct sections, namely minute (0-59), hour (0-23), day (1-31), month (1-12), weekday (0-6) and command. Sections are delimited by a space and sections 1 to 5 cannot have any spaces in it. Asterisks are wild cards and commas make up sets.

Thus for example we could add 01 04 1 1 1 /user/bin/somecommand, which would run /usr/bin/somecommand at 4:01am on January 1st plus every Monday in January.

It is suggested that you use full paths for the commands or scripts you want to execute.

Once you have added your new cron job lines, simple save the file and exit – the updated crontab will be immediately reloaded into memory.

And that is literally how easy it is to create a cron job!

Related Link: https://help.ubuntu.com/community/CronHowto

Ubuntu Crontab: How to Make a Scheduled Task run every couple of Minutes CodeUnit 01 JUN 2011

As a web developer you will always find a use for a scheduled job, something that runs in the background every now and then and does some sort of batch job to make life easier for someone interacting with your portal. If you are running a Ubuntu linux server, then the scheduled task (or scheduled job) becomes a “cron job”, and the crontab becomes your friend. So let’s create a cron job that runs every couple of minutes then, shall we?

First, access your crontab, using elevated privileges if you want (note that sudo crontab and crontab are actually two different job managers) with:

sudo crontab -e

Faced with the default text editor open up in front of you, you’ll remember that the linux crontab format is as such: [minute] [hour] [day of month] [month] [day of week] [command] and that asterisk (*) means every possible value.

Now given the command “/home/me/check-db-status”, we could schedule a job to run for every minute by simply inputting

* * * * * /home/me/check-db-status

However, this is pretty harsh on your poor server, so let’s say run it after every 20 minute interval. Our crontab entry now changes to:

*/20 * * * * /home/me/check-db-status

A small change, but effective. In this way you can set something to run every couple of minutes, say after every 5 (*/5) or 12 (*/12) minutes.

Nifty.

(But note that you can’t run something every couple of seconds because the lowest available unit is a minute!)

PHP: Optimize and Analyze All Your MySQL Tables CodeUnit 19 MAR 2010

20090910-MySQL-logoWhen you start working with sizeable datasets and the like, things like database optimization become more and more important for you to pay attention to.

So for today’s quick tutorial I present to you a script which can be used as either a cron or scheduled task and will run MySQL’s nifty analyze and optimize functions against all the tables in your database. Looking at it, you’ll see it is a pretty simple affair, basically we connect to our database in the usual fashion (note I’m simply using a connect function of my own design here) and then run a query to return the list of all tables in the selected database (again obscured by the connect function).

Once you have the list, it is a simple matter of looping through all the tables and executing the ANALYZE and OPTIMIZE statements against each table.

$conn = createconnection();

$alltables = mysql_query("SHOW TABLES");
while ($table = mysql_fetch_assoc($alltables))
{
   foreach ($table as $db => $tablename)
   {
       mysql_query("ANALYZE TABLE `".$tablename."`")
       or die(mysql_error());
       mysql_query("OPTIMIZE TABLE `".$tablename."`")
       or die(mysql_error());
   }
}
closeconnection($conn);

Simple, but effective.