Scheduling Jobs in Linux

  • 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, 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 like taking backups, which the clients usually want to be taken in nights, can easily be performed without the intervention of the administrator by scheduling a cron job.
  • If the cron job is scheduled carefully that 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 Red Hat or any other Linux, this process is handled by the cron service or a daemon called cron, which can be used to schedule tasks (also called jobs) By default, Red Hat comes with a set of predefined jobs that occur on the system (hourly, daily, weekly, monthly, and with arbitrary periodicity)
  • There are two tools for scheduling jobs.

             1. at

              2. cron tab

At Jobs

 “at” is used to schedule the job for a particular time or interval, in other words, it is used only for one time or only for one interval.

Syn: at [options]

  • l           lists 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.

Examples:

1. $ at 9am At> date Ctrl+d (save & quit)

2. $at now +3 days At> /bin/echo “hello world” Ctrl+d (save & quit)

3. $ at 03222013 At> ls Ctrl +d (save & quit)

4. $at 1:30     3/22/2013 At> cp file1  file2 Ctrl+d (Save & quit)

5. $at –f filename  11pm At> /bin/echo “hello world” Ctrl+d (save & quit)

Inclined to build a profession as Linux Developer? Then here is the blog post on, explore Linux Training

View the currently queued jobs:

$ at  - l (or) $ atq

Delete the job from the queue:

$ at – d 1 (or) $ atrm 1

Verify that the job is truly gone:

$at q

To View the job details:

$ at  -c 2

Restricting a user from using at jobs:

The atd service uses two files to control access to the services

/etc/at.allow /etc/at.deny The /etc/at.allow file:

If 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 (red hat default)

For both files:

If neither file exists, root only.

 

Linux Interview Questions

 

Cron jobs

The default setting for a red hat 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 jobs.

Syn: Crontab [-u use] [option]

Options:         

  • e         Edits the users crontab
  • l          Lists the user’s crontab
  • d         Deletes 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.  

To view the /etc/crontab file t understand its syntax:

  Automation jobs  

Examples:

# Crontab  –e

*/01    *          *          *          *          date

*/30    11        *          *          *          cp file1 file2

45        10,22   03        *          *          /bin/echo       “Hello world”

50        22        01,11   10        *          logname

59        23        31        12        *          /bin/echo       “Happy New Year” */01    *          *          *          0,6       /bin/echo       “Today is weekend” : wq !

To check assigned cronjobs:

# crontab –l

To check the cronjob service:

# service crond status

Restart the cron service:

#Service crond restart #Chkconfig crond on

To set up user 01’s crontab:

#corntab –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 fobs that should be run every time the system is started. If your system is turned off during the time that a cron job should 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 / etc/anacrontab

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 is permitted.

The /etccron.deny file:

If it exists and is empty, all users are allowed (red hat default)

For both files:

If neither file exists, root only.

For an in-depth understanding of Linux click on