- In any operating system, it is possible to create jobs that you want to reoccur. this process known as job scheduling, is usually done based on user-defined jobs.
- As an administrator however however you can define your own jobs and allow your users to create them as well.
- The importance of the job scheduling is that the critical tasks in nights, can easily be performed without the intervention of the administrator by scheduling a cron job. If the cronjob is scheduled carefully that the the backup will be taken at any given time of the client and there will be no need for the administrator to remain back at nights to take the backup.
- For RedHat or any other linux, this process is handle by the cron service or a daemon called cron, which can be used to schedule tasks (also called jobs).
- By default, RedHat comes with a set of predefined jobs that occur on the system with a set of predefined jobs that occur on the system (hourly, daily, weekly, monthly and with arbitrary periodicity)
- There are two tools to scheduling jobs
(a) at
(b) crontab
AT Jobs: ‘at’ is used schedule the job for a particular time or interval, in other words it is used only for one time or only for one interval.
Syntax:
at [options]
Options:
-l List all jobs in the queue
-d Removes a job from the queue
-f Reads input from the file
-m Sends mail to the user when the job is complete
Example:
$at 9am
at> date
ctrl+d
$at now + 3 days
at> /bin/echo " Webnoid "
ctrl+d
$at 03222013
at> ls
ctrl+d
$at 2:30 02/25/2022
at> cp file1 file2
ctrl+d
View the currently queued jobs:
$at -l
$atq
$at -f filename 110pm
at> /bin/echo "Hello Webnoid"
ctrl+d
Delete the job from the queue:
$at -d 1
Or
$atrm |
Verify that the job is truly gone:
$atq
To view the job details:
$at -c 2
Restricting a user from using at jobs:
The atd service uses two files control access to the service.
/etc/at . allow
/etc/at . deny
The /etc/at . allow file:
- It exists, only these users are allowed (at. deny is ignored).
- If it doesn’t exist, all users except at . deny are permitted.
The /etc/at . deny file:
If it exists and is empty, all users are allowed (RedHat default)
For doth files:
If neither file exists, root only.
cron jobs:
The default setting for RedHat allows any user to create a cron job. As the root user, you also have the ability to edit and remove any cron job you want.
Let’s jump into creating a cron job for the system you can use the crontab command to create, edit and delete job.
Syntax:
crontab [-u user] [options]
Options:
-e Edits the user’s crontab
-l Lists the user’s crontab
-r Delete the user’s crontab
-i prompts before deleting the user’s crontab.
Before you start using the crontab command, however, you should look over the format it uses so you understand how to create and edit cron jobs. Each user has her own crontab file in /var/spool/cron, based on the username of each user. Any “allow” actions taken by the cron service are logged to /var/log/cron.
crontab -e : To edit the cron
Example:
54 21 19 08 1 tar -cvzf /opt/home.tar.gz /home
54 =minute
21 =hour
19 =day
08 = month
01 =week
To check assigned cronjobs:
#crontab -l
To check the cronjob service:
#srevice crond status
Restart the cron service:
#service crond restart
#chkconfig crond on
To setup user01 crontab:
#crontab -u user01 -e
* * * * * /tmp/sample_script
: wq!
To remove a user’s crontab jobs:
#crontab -u user01 -r
You can verify the log file:
#tail /var/log/cron
Note: What do you think happens if you set up cron jobs to run during the night (say, to run some reports) and you shut down the system right before you go home? Well, it runs out that there is another great feature of cron. The /etc/anacrontab file defines job that should be run every time the system is started. If your have run, when the system boots again, the cron service will call /etc/anacrontab to make sure that all missed cron jobs are run.
Let’s look at the /etc/anacrontab file
#cat /etcanacrontab
Control access to the cron service:
To start working with cron, first need to know at the two config files that control access to the cron service.
These two files are:
/etc/cron . allow
/etc/cron . deny
The /etc/cron . allow file:
If it exists, only these users are allowed (cron. deny is ignored)
If it doesn’t exist, all users except cron . deny are permitted.
The /etc/cron . deny file:
If it exists and is empty, all users are allowed (RedHat default)
For both files:
If neither file exists, root only.