You are on page 1of 7

2.

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.

Cron and crontab:


The cron system is managed by the cron daemon. It gets information about which programs and
when they should run from the system's and users' crontab entries. Only the root user has access to the system
crontabs, while each user should only have access to his own crontabs. On some systems (some) users may not have
access to the cron facility.
At system startup the cron daemon searches /var/spool/cron/ for crontab entries which are named
after accounts in /etc/passwd, it searches /etc/cron.d/ and it searches /etc/crontab, then uses this information every
minute to check if there is something to be done. It executes commands as the user who owns the crontab file and
mails any output of commands to the owner.
Syntax
crontab [-e] [-l] [-r] [filename]
-e edit a copy of the current user's crontab file, or creates an empty file to edit if crontab
does not exist.
-l list the crontab file for the invoking user.
-r remove a user's crontab from the crontab
Filename The filename that contains the commands to run.

Lines that can be in the crontab file.


minute (0-59),
hour (0-23),
day of the month (1-31),
month of the year (1-12),
day of the week (0-6 with 0=Sunday).
Example:
crontab –e
fields,
min hour dayofmonth monthofyear dayofweek Command
0 12 14 2 * Echo “hai”
Options Explanation
* Is treated as a wild card. Meaning any possible value.
*/5
Is treated as ever 5 minutes, hours, days, or months. Replacing the 5 with another numerical value will change this
option.
2,4,6 Treated as an OR, so if placed in the hours, this could mean at 2, 4, or 6 o-clock.
9-17
Treats for any value between 9 and 17. So if placed in day of month this would be days 9 through 17. Or if put in
hours it would be between 9 and 5.

System Calls Related to Scheduling:

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.

Scheduling a job from the at prompt

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

at> job 4 at Tue Dec 19 11:29:00 2017

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.

Schedule the execution of a script

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:

# Using the dedicated -f flag

$ at now + 1 minute -f script.sh

# Using the < redirection operator $ at now + 1 minute < script.sh

Manage scheduled jobs


To queue, examine or delete jobs scheduled with we can either use dedicated commands like atrm and atq or
run at with specific flags, the latter being just aliases for the former. For example, say we want to obtain a list of all
pending jobs scheduled with at by our user:

$ atq

4 Tue Dec 19 11:29:00 2017 a egdoc

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 Syntax

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]

 Astrics (*) – Matches anything


 Define range – You can define range using the hypen like: 1-10 or 20-30 or sun-fri or feb-apr
 Define multiple range – You can define multiple ranges with command seprated like: jan-mar,jul-sep

How to Add/Edit Crontab

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

Change EDITOR environment variable to change your default editor.

How to List Crontab

To view crontab entries of current user use the following command.

crontab -l

Use -u followed by username to view crontab entries of the specified user.


crontab -u username -l

20 Useful Crontab Examples

Here

1. Schedule a cron to execute at 2am daily.


This will be useful for scheduling database backup on daily basis.

0 2 * * * /bin/sh backup.sh

 * are used for matching all the records.


2. Schedule a cron to execute twice a day.
Below example command will execute at 5 AM and 5 PM daily. You can specify multiple time stamp by comma
separated.

0 5,17 * * * /scripts/script.sh

3. Schedule a cron to execute on every minutes.


Generally, we don’t require any script to execute on every minute but in some case, you may need to configure it.

* * * * * /scripts/script.sh

4. Schedule a cron to execute on every Sunday at 5 PM.


This type of cron is useful for doing weekly tasks, like log rotation etc.

0 17 * * sun /scripts/script.sh

5. Schedule a cron to execute on every 10 minutes.


If you want to run your script on 10 minutes interval, can configure like below. These type of crons are useful for
monitoring.

*/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.

6. Schedule a cron to execute on selected months.


Sometimes we required scheduling a task to be executed for selected months only. Below example script will run in
January, May and August months.

* * * jan,may,aug * /script/script.sh

7. Schedule a cron to execute on selected days.


If you required scheduling a task to be executed for selected days only. Below example will run on each Sunday and
Friday at 5 PM.

0 17 * * sun,fri /script/script.sh

9. Schedule a cron to execute on every four hours.


If you want to run a script on 4 hours interval. It can be configured like below.
0 */4 * * * /scripts/script.sh

10. Schedule a cron to execute twice on every Sunday and Monday.


To schedule a task to execute twice on Sunday and Monday only. Use following settings to do it.

0 4,17 * * sun,mon /scripts/script.sh

11. Schedule a cron to execute on every 30 Seconds.


To schedule a task to execute on every 30 seconds is not possible by time parameters, But it can be done by schedule
same cron twice like below.

* * * * * /scripts/script.sh

* * * * * sleep 30; /scripts/script.sh

12. Schedule a multiple tasks in single cron.


To configure multiple tasks with single cron, Can be done by separating tasks by the semicolon ( ; ).

* * * * * /scripts/script.sh; /scripts/scrit2.sh

13. Schedule tasks to execute on yearly ( @yearly ).


@yearly timestamp is similar to “0 0 1 1 *”. It will execute task on the first minute of every year, It may useful to

send new year greetings

@yearly /scripts/script.sh

14. Schedule tasks to execute on monthly ( @monthly ).


@monthly timestamp is similar to “0 0 1 * *”. It will execute a task in the first minute of the month. It may useful to
do monthly tasks like paying the bills and invoicing to customers.

@monthly /scripts/script.sh

15. Schedule tasks to execute on Weekly ( @weekly ).


@weekly timestamp is similar to “0 0 1 * mon”. It will execute a task in the first minute of the week. It may useful
to do weekly tasks like the cleanup of system etc.

@weekly /bin/script.sh

16. Schedule tasks to execute on daily ( @daily ).


@daily timestamp is similar to “0 0 * * *”. It will execute a task in the first minute of every day, It may useful to do
daily tasks.

@daily /scripts/script.sh

17. Schedule tasks to execute on hourly ( @hourly ).


@hourly timestamp is similar to “0 * * * *”. It will execute a task in the first minute of every hour, It may useful to
do hourly tasks.
@hourly /scripts/script.sh

CREATE TABLE setTest(


attrib SET('bold','italic','underline')
);

INSERT INTO setTest (attrib) VALUES ('bold');


INSERT INTO setTest (attrib) VALUES ('bold,italic');
INSERT INTO setTest (attrib) VALUES ('bold,italic,underline');
You can copy the code above and paste it in mysql, and you will find that SET actually is a collection. You can
store each combine of attributes you declare.
CREATE TABLE enumTest(
color ENUM('red','green','blue')
);

INSERT INTO enumTest (color) VALUES ('red');


INSERT INTO enumTest (color) VALUES ('gray');
INSERT INTO enumTest (color) VALUES ('red,green');

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

You might also like