Professional Documents
Culture Documents
Scheduling
Scheduling
2 Scheduling:
A Linux system can have a lot to suffer from, but it usually suffers only during office hours. Whether in an office
environment, a server room or at home, most Linux systems are just idling away during the morning, the evening,
the nights and weekends. Using this idle time can be a lot cheaper than buying those machines you'd absolutely need
if you want everything done at the same time.
There are three types of delayed execution:
Waiting a little while and then resuming job execution, using the sleep command. Execution time depends on
the system time at the moment of submission.
Running a command at a specified time, using the at command. Execution of the job(s) depends on system time,
not the time of submission.
Regularly running a command on a monthly, weekly, daily or hourly basis, using the cron facilities.
The at command:
The at command executes commands at a given time, using your default shell unless you tell the
command.The options to at are rather user-friendly, which is demonstrated in the examples below:
aravind@home:~> at tomorrow + 2 days
warning: commands will be executed using (in order) a) $SHELL
b) login shell c) /bin/sh
at> cat reports | mail myboss@mycompany
at> <EOT>
job 1 at 2010-10-16 12:36
Typing Ctrl+D quits the at utility and generates the "EOT" message.
User aravind does a strange thing here combining two commands
aravind@home:~> at 0237
warning: commands will be executed using (in order) a) $SHELL
b) login shell c) /bin/sh
at> cd new-programs
at> ./configure; make
at> <EOT>
job 2 at 2010-10-14 02:00
The -m option sends mail to the user when the job is done, or explains when a job can't be done.
Thecommand atq lists jobs; perform this command before submitting jobs in order prevent them from starting at the
same time as others. With the atrm command you can remove scheduled jobs if you change your mind.
The at command is used to schedule one or more programs for a single execution at some later time.
There are actually four client commands:
1. at: Runs commands at specified time
2. atq: Lists pending commands
3. atrm: Cancels pending jobs
4. batch: Runs commands when system load permits
The Linux at command accepts a number of time specifications, considerably extending the POSIX.2 standard.
We change the scheduling parameters by means of the system calls illustrated in the Table
Table 10-1: System Calls Related to Scheduling
System Call Description
nice( ) Change the priority of a conventional process.
getpriority( ) Get the maximum priority of a group of conventional processes.
setpriority( ) Set the priority of a group of conventional processes.
sched_getscheduler( ) Get the scheduling policy of a process.
sched_setscheduler( ) Set the scheduling policy and priority of a process.
sched_getparam( ) Get the scheduling priority of a process.
sched_setparam( ) Set the priority of a process.
sched_yield( ) Relinquish the processor voluntarily without blocking.
sched_get_ priority_min( ) Get the minimum priority value for a policy.
sched_get_ priority_max( ) Get the maximum priority value for a policy.
sched_rr_get_interval( ) Get the time quantum value for the Round Robin policy.
Most system calls shown in the table apply to real-time processes, thus allowing users to develop real-time
applications.
With everything in place, we can now use at. Let's suppose we want to run a command 1 minute from now. The
correct syntax would be:
$ at now + 1 minute
To run the same command at 4pm, three days from now, instead, we would run:
$ at 4pm + 3 days
Once the above line is executed, the at prompt will appear, waiting for us to enter the command to be executed after
the specified time interval:
$ at now + 1 minutes
at> echo "Hello world" > test.txt
To exit the at prompt we should press the CTRL+d key combination. At this point we will presented with a
summary of the scheduled task, which will show us the job id (4 in this case) and the date at which it will be
executed.
Just as an example, we entered a trivial command to show how at works. A minute from now, the "Hello world"
string will be written to the file test.txt, which will be automatically created if doesn't already exist.
Instead of specifying the command to be executed, interactively, from the prompt, we can instruct at to execute an
existing script or program simply by passing it as an argument to the -f flag or, alternatively, by using
the < redirection operator. Therefore, assuming we want to run a script which is present in our current working
directory, we would run:
$ atq
The above command, if launched as root, will display the task scheduled by all users in the system.
To delete a queued job, we could use atrm or run at with the equivalent flags: -r or -d. The job to be deleted must be
referenced by its number. In the case above, we would therefore run:
$ atrm 4
Conclusions
Although simpler than cron , the at program can be very useful in certain situations: to run a program with a specific
delay or when you know exactly the time in which the task must be executed.
Crontab
The crontab is used for running specific tasks on a regular interval. Linux crontab is similar to windows task
schedules. Crontab is very useful for routine tasks like scheduling system scanning, daily backups etc. Crontab
executes jobs automatically in the backend on a specified time and interval.
Linux crontab has six fields. 1-5 fields defines the date and time of execution. The 6’th fields are used for command
or script to be executed.The Linux crontab syntax are as following:
[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
To add or update job in crontab, use below command. It will open crontab file in the editor where a job can be
added/updated.
crontab -e
By default, it will edit crontab entries of current logged in user. To edit other user crontab use command as below
crontab -u username -e
crontab -l
Here
0 2 * * * /bin/sh backup.sh
0 5,17 * * * /scripts/script.sh
* * * * * /scripts/script.sh
0 17 * * sun /scripts/script.sh
*/10 * * * * /scripts/monitor.sh
*/10: means to run on every 10 minutes. Same as if you want to execute on every 5 minutes use */5.
* * * jan,may,aug * /script/script.sh
0 17 * * sun,fri /script/script.sh
* * * * * /scripts/script.sh
* * * * * /scripts/script.sh; /scripts/scrit2.sh
@yearly /scripts/script.sh
@monthly /scripts/script.sh
@weekly /bin/script.sh
@daily /scripts/script.sh
you can also copy the code above. And you will find that each ENUMactually can only be store once each time.
And you will find that the results of last 2 lines will both be empty