In this article we are going to look at how to schedule tasks using the at daemon and the cron daemon, we will also look into limiting what users can schedule tasks. The cron and 'at' daemon are both used for scheduling tasks but are both used different. For example, the at daemon is used to schedule a one off task whereas the cron daemon is use to schedule a task that happens more than once.
In this section of the article we will look at scheduling a task using the at command. The at daemon is used execute a one off task on a specified date, at a certain time or both. The at command when executed takes one argument which is the date and/or the time as an argument and then drops you into the at prompt as shown in Figure 2.1.
linux-zuew:~ # at 11:35AM
Figure 2.1: The 'at' command prompt.
Once you are at the 'at' command prompt you can start to type a list of commands you would like to execute, the commands that you enter at the 'at' command prompt are executed sequentially. In this article we will do a simple test which will broadcast a message at 11:45AM saying "Hello". Figure 2.2 shows the command used to display the message at 11:45AM.
linux-zuew:~ # at 11:45
at> wall "Hello"
job 5 at Tue Jul 15 11:45:00 2008
Figure 2.2: Specifying a simple job using the 'at' command.
As you can see from Figure 2.2 the first and only command is wall "Hello" which will send a message to all users on the system. Once you have written the first command you will need to press the key combination 'Ctrl + D' which will exit and save the scheduled task.
The second example that we will do is schedule a task for the 28th of July 2008 and at 11:50AM saying “Hello” to every user on the system the command used is shown in Figure 2.3.
linux-zuew:~ # at 11:50AM July 28
at> wall "Hello"
job 6 at Mon Jul 28 11:50:00 2008
Figure 2.3: Specifying a command for a certain month and at a certain time.
As you can see in Figure 2.3 when you exit the 'at' prompt you are notified of the job ID and when the task will be executed. There is command that can be used to query the 'at' daemon which will display the job ID, the date and time in which the task will be executed and who is responsible for that task. The command used to query the 'at' daemon is atq as shown in Figure 2.4.
linux-zuew:~ # atq
7 Wed Jul 28 11:50:00 2010 a damian
6 Mon Jul 28 11:50:00 2008 a root
Figure 2.4: Display all current tasks awaiting execution.
Controlling at Access
The 'at' daemon controls access to scheduling via two configuration files 'at.deny' and 'at.allow' these two configuration file are located within the /etc directory. The access control for the 'at' daemon is very simple, the 'at.deny' configuration file is used to deny access to the 'at' daemon, users are specified on separate lines. The 'at.allow' configuration file is used to specify the users who are allowed to to schedule tasks if users are not specified within the file they will not be able to schedule tasks.
Removing scheduled tasks
Removing scheduled tasks is simple you can simply issue the atrm command followed by the job ID which will remove the scheduled task. Once you have removed the scheduled task you can issue the atq command to query the 'at' daemon to make sure it was successfully removed.
One of the common problems you might have with scheduling tasks using the at daemon is that the 'at' daemon might not be running, you can check the status of the 'at' daemon by issuing the service command followed by 'atd status' as shown in Figure 2.5.
linux-zuew:~ # service atd status
Checking for at daemon: unused
Figure 2.5: Checking the 'at' daemon status.
If the 'at' daemon is not running you can issue the service atd start command to start the 'at' daemon.
The cron daemon is used for scheduling tasks that occur more than once, whereas the 'at' daemon is usually used for a one off task. The cron daemon is a little more tricker to use as you have to specify the minute, hour, week, day of the month, month, day of the week, username (optional) and the command. Figure 3.1 shows the syntax of a cron entry.
5 * * * * /usr/bin/wall "Hello"
Figure 3.1: Cron job entry.
The cron entry shown in Figure 3.1 executes every five minutes past the hour every day. Table 1 explains what each field accepts and Table 2 shows the possible entries that you can make.
||The minutes column can range from 0 to 59
||The hours column can range from 0 to 23
|Day of the month
||The day of the month can range from 1 to 31
||The month column can range from 1 to 12
|Day of the week
||The day of the week column can range from 0 to 7. 0 is equivalent to Sunday and 7 is also equivalent to Sunday.
||The username column specifies who the cron job should run as. The username is also optional.
||The command column is the actual command you would like to execute.
Table 1: Cron fields explained.
||This entry is a wild card entry and means any number.
||This entry specifies a range so the number that are actual specified are 1, 2, 3, 4 and 5.
||This entry specifies a list of numbers so at each number you can execute a specific command.
Table 2: Cron supported entries.
Once you understand what fields are required and what wild cards are supported we can begin writing some basic cron jobs. However, the first task you need to do is open the cron editor this can be done by issuing the crontab -e command. Once you have opened the cron editor you can begin writing your cron entires, the editor used by cron is usually vim so you can press the 'i' character to change into inert mode, when you have finish writing you cron job you can press the escape key followed by :wq this will save the changes and exit the editor.
The first example we will do is run the wall command on the 1st and 3rd minute of every hour, Figure 3.2 shows the cron entry.
1,3 * * * * wall "Hello"
Figure 3.2: Runs the wall command every 1st and 3rd minute of every hour.
The use of the wall command is very good as it lets you actually see when the cron job is running because it broadcasts the message "Hello" to everyone. The next example we will do is run the wall command every 1st and 3rd minute at 16:00 hours as shown in Figure 3.3.
1,3 16 * * * wall "Hello"
Figure 3.3: Execute the wall command at 16:01 and 16:03.
The final example we will do is execute the wall command on January the 28th at 16:01 and 16:03 as shown in Figure 3.4.
1,3 16 28 1 * wall "Hello"
Figure 3.4: Executing the wall command on the 28th of January at 16:01 and 16:03.
The two configuration files that control user access to the cron daemon are 'cron.allow' and 'cron.deny' these two configuration files are located within the /etc directory and are similar to the 'at.deny' and 'at.allow' configuration files.
The 'cron.allow' file is used to allows users to schedule jobs using the cron daemon, if the username does not exist in the 'cron.allow' configuration file the user will be denied access. The 'cron.deny' configuration file houses the username of users that are not allowed to schedule jobs, both configuration files have the same syntax as the 'at.deny' and 'at.allow' configuration files e.g. Each username should be specified on an individual line.
Cron – Hourly, Daily, Weekly and Monthly
If you have a job that requires to be executed hourly, daily, weekly or monthly but you don't mine when the job is ran you can place a script within the '/etc/cron.hourly', '/etc/cron.daily', '/etc/cron.weekly' or '/etc/cron.monthly' directory. The cron directories usually contain a set of bash scripts that are executed according to what directory they are in.
In this article we have cover how to schedule tasks using two different methods, hopefully you found this article helpful. I would also recommend reading the cron or 'at' manual pages if you still have any uncertainties or you can leave a comment.
Disclaimer: As with everything else at Cool Solutions, this content is definitely not supported by Novell (so don't even think of calling Support if you try something and it blows up).
It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.