- [Instructor] Hi and welcome to the next video
of the last section.
Scheduling with cron.
In the previous video, we've seen about using slash proc
for gathering information.
In this video, we'll see how to schedule with cron.
It's a common requirement to schedule execution of scripts
given time or a given time intervals.
The GNU/Linux system comes with different utilities
for scheduling tasks.
Cron is such a utility that allows tasks
to be automatically run in the background of the system
at regular intervals using the cron daemon.
The cron utility makes use of a file called cron table
that stores a list of schedules of scripts or commands
to be executed and the time at which
they ought to be executed.
A common example usage is that you can schedule downloads
of files from the internet during free hours.
Certain ISPs provide free usage hours,
usually at night time.
This way you won't be required to wake up in the night
to start the download.
In addition to writing a cron entry
and schedule of a download, you can also schedule
to drop the internet connection automatically
and shutdown the system when free usage hours ends.
The cron scheduling utility comes with
all GNU/Linux distributions by default.
Once we write he cron table entry, the command will be
executed at the time specified for execution.
The command crontab is used to add jobs to the cron table.
The cron table is a simple text file,
and each user has a separate copy.
In order to schedule tasks,
we should know the format for writing the cron table.
A cron job specifies the path of a script or command
to be executed, and the time at which it is to be executed.
Cron job to execute the test sh script at the second minute
of all hours on all days.
In order to run the script at the fifth, sixth,
and seventh hours on all days, use.
Execute script.sh at every hour on Sunday as follows.
Shutdown the computer at 2 A.M. everyday as follows.
Now let's see how to schedule a cron job.
You can execute the crontab commands in multiple ways
to schedule the scripts.
Use the minus e option to crontab
to start editing the cron table.
When crontab minus e is entered,
the default text editor, usually vi, is opened up.
And the user can type the cron jobs and save it.
The cron job will be scheduled
and executed at specified time intervals.
There are two other methods we usually use
when we invoke the crontab command inside a script
for scheduling tasks.
Create a text file.
For example task.cron with the cron job in it.
And then run the crontab file with this filename
as the command argument.
Or specify the cronjob inline
without creating a separate file, for example.
The cron job needs to be written in between crontab,
EOF, and EOF.
The cron table consists of six sections
in the following order.
Minute, zero to 59.
Hour, zero to 23.
Day, one to 31.
Month, one to 12.
Weekday, zero to six.
Command, the script or command to be executed
at the specified time.
The first five sections specify the time at which
an instance of the command is to be executed.
There are a few additional options
to specify the time schedule.
An asterisk is used to specify that the command
should be executed at every instance of time.
That is, if star is written in the hour field
in the cron job, the command will be executed every hour.
Similarly, if you would like to execute the command
at multiple instances of a particular time period,
specify the time period separated by a comma
in the corresponding time field.
For example, if we're running the command
at the fifth minute and 10th minute,
enter five comma 10 in the minute field.
We also have another nice option
to run the command at particular divisions of time.
Use star slash five in the minutes field
for running the command every five minutes.
We can apply this to any time field.
A cron table can consist of one or more lines of cron jobs,
and each line in the cron table is a single job.
Cron jobs are executed with privileges
with which the crontab command was executed.
If you need to execute commands
that require higher privileges,
such as command for shutting down the computer,
run the crontab command as root.
The command specified in a cron job
are written with the full path to the command.
This is because the environment
in which a cron job is executed,
is different from the one we execute on a terminal.
Hence the path environment variable may not be set.
If your command requires a certain environment variable
to be set for running,
you should explicitly set the environment variables.
The crontab command has more options.
Let's see a few of them.
Specifying environment variables.
Many of the commands require environment variables
to be set properly for execution.
We can set environment variables
by inserting a line with a variable assignment statement
in the cron table of the user.
For example, if you're using a proxy server
for connecting to the internet,
to schedule a command that uses the internet,
you have to set the HTTP proxy environment variable.
HTTP underscore proxy.
It can be done as follows.
Running commands at system startup slash boot.
Running specific commands when the system starts or boots,
is a common requirement at times.
There are a lot of ways to achieve this,
and using cron is one of them.
The others being adding your commands to slash etc,
slash rc dot d.
But that's not guaranteed to be the same across distros.
To run a command at boot,
add the following line to your crontab.
This will run the command as your user at runtime.
To run the command as root,
edit the root crontab.
Viewing the cron table, we can list these existing cron jobs
using the minus l option.
The crontab minus l lists the existing entries
in the cron table for the current user.
We can also view the cron table for other users
by specifying a username with the minus u option as follows.
You should run as root when you use the minus u option
to gain higher privilege.
Removing the cron table.
We can remove the cron table for the current user
by using the minus r option.
In order to remove crontab for another user, use.
Run as root to get higher privilege.
Also, we've successfully learned
how to schedule with cron.
In the next video, we will learn how to write and read
the MySQL database from Bash.