Friday, 24 October 2014

An Everyday Linux User Guide To Scheduling Tasks Within Linux

Posted by Gary Newell  |  at  23:16 6 comments

Introduction

Sometimes when I get involved in a task I lose track of time. This can have really bad consequences if left unchecked such as forgetting to pick up my daughter from the bus stop or forgetting to take stuff out of the oven.

I am also the type of person that becomes so involved in their work that I can easily forget to stop for food and neglect to take breaks.

If I know I am going to be working from home I now set up a job to pop up a message at certain points in the day to remind me to do something.

To schedule tasks in Linux there are two useful tools that are worth knowing about:
  • at
  • cron
In this guide I am briefly going to touch upon these two commands and then I am going to show you the Everyday Linux User way which I find to be much easier.

Scheduling A Job To Occur Once Using at

The at command makes it possible to run a task in the future. For instance using the examples from my introduction I might want to set a task to run at 3 PM to remind me to pick up my daughter from the bus.

There are various tools for sending a message to the screen including xmessage which is very basic and the one I like to use which is kind of novel called xcowsay.

xcowsay displays a picture of a cow and the message you would like displayed within a speech bubble.


The syntax for the at command is as follows:

at <time>
> command

There are various ways to specify the time such as by specifying the hours and minutes or by specifying midnight, noon or teatime. You can specify to run the command today, tomorrow, in 5 days time or 5 weeks from now.

Here are a few examples for setting the time you want to run a scheduled task:

  • at 9:45 PM - runs command at 9:45 pm
  • at 10:00 AM - runs command at 10:00 am
  • at 9:30 PM tomorrow - runs command at 9:30 pm tomorrow
  • at 4:00 PM + 3 days - runs command 3 days from now at 4 pm
  • at 10:00 AM Jun 25 - runs command at 10 am on June 25th
The command element can be any command but to display output to the screen the command needs to know which screen to send it to.

Therefore to run a program such as xcowsay you would do the following:

at 11:00 PM
> export DISPLAY=:0 && xcowsay "Time for bed"

This command will run at 11 PM. The export display=:0 tells the job to display output to the screen and xcowsay displays the text "Time for bed" in a speech bubble coming from a cartoon cow.

The at command is useful if you want to run a command in the future only once.

If you want to see all the commands that are set to run in the future type atq

10 Fri Oct 24 22:09:00 2014 a gary

Of course the at command can be used for much more powerful things than just displaying messages. 

If you want to remove a scheduled task run the atrm <jobid>. The job id can be seen by running the atq command. The job ID is the number on the left.

Scheduling A Recurring Job Using cron

cron is useful for scheduling jobs that are to occur again and again.


For instance, if I work from home between Monday and Friday I might want to be notified when it is lunchtime and to schedule drinks breaks.

To schedule a task you have to use the crontab -e command which opens a file as follows:



The command in the above screenshot displays the message "Have lunch" every weekday at 12:30.

The timings look a bit weird when you first see them but basically from left to right they are:


  • Minutes
  • Hours
  • Day Of Month
  • Month
  • Day Of Week
If you look at the command in the screenshot again you will see it looks as follows:

30 12 * * 1-5 command

So the first number as shown by the key above is minutes. That means at 30 minutes past the hour the command specified is going to run.

If the command was 30 * * * * then the command would run 30 minutes past every hour on every day.

The second number in the key above is hours. That means that at 12:30 the command will run.

Again if the command was 30 12 * * * the command would run at 12:30 every day.

The third number is the day of the month. In the command above it is set to * which means every day.

The fourth value is the month. In the command above it is set to * which means every month. You can specify any month number between 1 and 12.

Finally the 5th number is the day of the week. Therefore if you want the command to run every Monday you specify 1.

As you can see my 5th number is set to 1-5. This means the command will run on every day between 1 and 5. (i.e. Monday, Tuesday, Wednesday, Thursday, Friday).

If you want a command to run every 5 minutes between 9 AM and 5 PM every day you would use the following syntax:

*/5 9-17 * * * command

The /n (where n is the number to skip by) means the command will run all the time but skip by the number specified by n. 

If you want to run a command at multiple times in the day you can use a comma to separate the times as follows:

5 12,15,18 * * * command

The above command will run at 12:05,  15:05 and 18:05.

Saving the file and exiting the program automatically updates cron and the events will start firing as and when they are supposed to.

Using corntab to schedule a task

The corntab website provides a visual crontab editor for helping to schedule cron jobs.

This makes it easier to add recurring jobs without remembering the rules from the previous section.

 
All you have to do is select each category (minute, hour, day of month, month and day of week) and visually select the values you are looking for. You can enter the command you want to run in a dedicated text box.

This provides you with the command that you need to paste into the crontab editor.


















As good as corntab is there is an even easier way to schedule tasks.

Schedule Tasks Using gnome-schedule
















The easiest way to schedule tasks is to use Gnome Schedule (gnome-schedule)

Gnome Schedule can be found within the package managers of most distributions.

When you run gnome-schedule a list of tasks will appear. The tasks can be one-off tasks or recurring.

To add a new task click on "New".


You can now choose whether to set up a recurring task or a one off task.

If you click on the "A task that launches recurrently" button the following screen appears:


The description field makes it easier to differentiate between recurring tasks. 

The command field is used to enter the command that you want to run. You can also choose whether the command is to run as an X application or not.

There are two ways to define the time and date. You can choose the basic option which lets you specify whether the task runs every minute, every hour, every day, every month or on reboot. The advanced option lets you enter the specific minute, hour, day, month and weekday. 

The rules for the advanced section are the same as for entering manually into the crontab file. The good thing about the graphical tool is that it gives you a preview telling you exactly when the command will run.

Click "Add" to add the command to the list of recurring tasks.



To schedule a one-off task click on the "New" button and select "A task that launches one time".

When the window above appears enter a description of the task and choose whether it is an X application or not.

Select a date and time to run the command and enter the task within the task window.

Click "Add" to schedule the task.

Summary

Scheduling tasks is made much easier by using the Gnome scheduling tool.

The examples I have given above are very simplistic and in reality cron jobs are used for much more sophisticated tasks such as scheduling backups.

Cron jobs are more useful on computers that are kept on for long periods of time but can be useful for providing reminders during the day.

It is worth reading the manuals for cron (man cron) and at (man at)

I hope you found this guide useful. 




Tags:

    Popular Posts



back to top