Tag Archives: crontab

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

XBMCbuntu: How to Automatically Shutdown your PC Tips, Tricks and Tutorials 03 JUL 2014

Because XBMCbuntu is essentially a light-weight Ubuntu install configured to run XBMC at startup, you have access to most of the basic commands that a normal Ubuntu box would have. In other words, you have access to all the normal niceties of Ubuntu – like the crontab, the perfect way of scheduling an automatic shutdown of your XBMC media server. You know, because a) you like the idea of saving electricity, or b) your old hardware powering your media server is a little… well noisy.

Anyway, bring up a terminal by pressing ALT+CTRL+F1. Your login details will be those that you setup during install. If you don’t have those on hand, then you could try the default XMBC root user which has the details of username=’xbmc’ and password=” (blank).

Now to edit the crontab (as sudo of course) to set up our scheduled task:

sudo crontab -e

Add the shutdown rule as follows. In this case, it is set to run the shutdown command every evening at 1 o’clock in the morning – everyone should be asleep by then, movie nights done and dusted for sure! (Obviously you can set whatever times you want, play around a little if you must.)

0 1 * * * /sbin/shutdown -P now

Save the crontab and on exit of the editor, you should see the line saying “Installing new crontab”. You can obviously test the command yourself right then and there by running:

sudo /sbin/shutdown -P now

Although this will of course shutdown your machine. If you just want to jump back to XBMC, hit ALT+CTRL+F7.

A useful tip if your small child isn’t quite capable of controlling XBMC just yet! :)

xbmc-logo-banner

Related Link: http://wiki.xbmc.org/?title=XBMCbuntu

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

How to add a Comment or Remark to a Crontab in Ubuntu CodeUnit 08 JUN 2012

To add a comment or remark in your Ubuntu crontab is trivially easy, following the standard for adding comments already found in bash scripting.

In other words, all you have to do in order to add a line which you don’t want the cron system to process, is start it off with the hash (#) character!

In practice:

# This is a comment and will not be processed by the cron engine
00 01 * * * ls -lh /home/craig

Nifty.

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!)

Ubuntu: How to Restart the Cron Service CodeUnit 05 JAN 2011

After making changes to your crontab file, be it the sudo or standard version of the file, you will need to restart the appropriate cron service. The command line sequence to achieve this is as follows:

sudo /etc/init.d/cron restart

The system should indicate that the cron has been successfully restarted, and you can now simply sit back and relax – job done! :)

Ubuntu Terminal: How to Back up Your Crontab File CodeUnit 24 DEC 2010

To generate a back up of a crontab file is actually pretty simple, making use of the built in -l switch that comes with the command:

sudo crontab -l > ~/path/to/your/backup/file

See what we did there? The -l switch spits out the crontab file’s contents and we simply pipe that into a file. Easy peasy. Note that sudo crontab and crontab are two different files, meaning you might want to backup both if you make use of the two different cron job files…