Featured

    Featured Posts

Scheduling jobs using ' at ' and ' cron '

If you try to execute any command in linux/unix terminal, it is for one time execution and it will get executed once it is invoked.

Suppose if you have a command/script which should be executed at particular time/period OR at regular intervals of time... is it possible ?

Yes. it is possible!

We have a commands  "at" and "crontab" which were used for scheduling one time job and regular interval of time (recurring jobs)  respectively.

For ex: If you want to remove files in /tmp at evening 4pm today. You can automate this using "at" job . i.e., One time scheduler.

Suppose if the same task should be performed on everyday at 4pm , you can make use of "crontab" which is used to schedule the recurring jobs.


Scheduling jobs:

    at              ⇒  One time job scheduler ; Executes commands at specified time.
    crontab     ⇒  Recurring jobs ; it is a deamon to execute scheduled commands.


--------------------------------------------------------------------------------------------------------------------------------------------------------

at :

Creating jobs using “at” :
Syntax:    #at  time                                                       [here time is mandatory]
                 at >commands                                           
                 at >commands
                 at >ctrl+d                                                      [ctrl+d is for save]


Note: If you dont specify any time, then it will give grabled time error.
[root@myserver ~]# at
Garbled time
[root@myserver ~]# 


Few examples:
#at 8:31                                [it will schedule the job at 8:31am to remove all files under /tmp]
at> rm –rf   /tmp/*
at> ctrl+d 


Sample at job scheduling:
#at 7am
#at 11:30
#at noon
#at tea time                        → schedules job at evening 4pm or 16:00
#at now+10mits                  →  after 10mins from  current time  
#at 8pm + 3days
#at now + 2hours
#at 7pm monday          
#at 8am tomorrow
#at 8pm december 25


To see pending jobs:
# atq      (or)    # at -l

To remove jobs:
# at  -d  jobno                (OR)          #atrm jobno

ex: # atrm 4       → 4th job will be removed/deleted


To see job details:
# at -c jobno

Few important points to remember:
  • If you set the job for the which is already passed then it will be scheduling at next day Ex: current time is 10:00am if we scheduled at 7:00am then it will run on tomorrow             7:00am
  • Editing (or)modifying at job is not possible after we set it. We need to delete job and recreate it.
  • If the system is down but job exist at the time then once the syatem is up, immediately the job will run
  • We can see our own job using  # atq
  • Other user jobs cannot be seen. only the root user can see all jobs of users.
  • After executing “at” job it will be deleted.
  • Non redirected output will be redirected to mail
eg: in place of commands if you give
               echo "helloooo"
then the output ont be displayed on terminal rather the outpout will be sent to user mail.
if you want to display the output on terminal, then use terminal name
eg:     echo "helloooo" > /dev/tty2                             [to verify tty name : use tty command]

To allow and Deny the user to create crontab:

/etc/at.deny    :  To deny  the user to create “at” job, we need to make an entry of username  into /etc/at.denny

Vi /etc/at.deny
kiran
:wq!

Now system wont allow kiran to create at jobs, it will show permission denied.

# /etc/at.allow    : Block/restrict all current and future users otherthan one user.

Note: by default /etc/at.allow will be empty.
Be careful, if you update /etc/at.allow with any name, remaining all users will be denied to use at jobs.
--------------------------------------------------------------------------------------------------------------------------------------------------------
crontab:

Recurring Jobs: 

crontab:  It is used to schedule the  jobs on daily basis.
                It stands for cron tables
                It consists fo 6 fields. Out of which 5 are time and date related and last filed (6th                     filed) is either command/script to be executed.
 

below is the commad to know crontab fileds:
# cat /etc/crontab 


Here
Minutes              -     0 to 59
Hours                 -    0 to 23
Day of Month     -    1-31
Month                 -     jan-oct or  1-12
Day of Week      -    0-sun ; 1-mon; 6-sat

Commands:

crontab -e
To edit the crontab if exist or create a new one
crontab -l
To display existing crontab.
crontab -r
To remove existing crontab.
crontab  -e -u  user
To set/edit the crontab for another user. (Only root can do this)


Note:
  • An " * " in a fiels represtent all possible values.
  • Multiple values may be seperated by " , " (comma)
  • Ranges can be seperated by " - "
  • Seconds is not possible. Default is atleast a minute. ( you can write script to schedule for a second)
  • If we specify */5 in a minutes fields , it means every 5 minutes
  • 0-10/2 in a minutes fields means, every 2 minutes in  1 to 10 minutes 
  • Root can set crontab for any users , normal users can set crontab for only himself
Examples:

1. Every month; Every monday & Friday ; at 10:40am ; execute a job.
     45               10               *                 *                  1,5                commands

2. January & March; Monday to Friday; 13:40 ; execute a job
     40                13               *                1,3               1-5               commands

3.Every new year starting time ; execute a job
     0                    0               1                 1                  *                   commands

4. Every Month starting time; execute a job
     0                    0               1                 *                  *                   commands
 5 5.
5. Every Day starting time; execute a job
     0                    0               *                  *                  *                  commands

 6. Weekly starting time ; execute a job
0                    0               *                       *                  1                   commands
 kjklfkdlsjlkdksjfklds
7.Every 2 hours ; execute a job
     0                    */2             *                   *                 *                   commands


Few more easy commands:

Fields
Usage
Entries
@reboot

Execute the command immediately after reboot.(once system is UP)
        @reboot          commands
@hourly

To run once an hour
0   *   *   *   *  commands
@daily
To run once a day
0   0   *   *   * commands

@weekly

To run once a week
0   0   *   *   0  commands
@monthly

To run once a month
0   0   1   *   *  commands
@yearly
To run once a year

0    0   1   1   *  commands

Usages of crontab:
To take regular backups, scheduled updates, copying/moving/deleting files, displaying notifications etc.,






--------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks 😊
author

Gnanasekhar Reddy

-----------

Get Free Email Updates to your Inbox!

2 comments

Thanks for sharing such wonderful information about Jobs in Linux>.I read your articles very excellent and the i agree our all points because all is very good information provided this through in the post.

Reply

You are most welcome & Thank you for your valuable feedback :)

Reply

Post a Comment

www.CodeNirvana.in

COPYRIGHT © 2017. Powered by Blogger.

Featured Posts

Featured Posts

Featured Posts

Popular Posts

Translate

Total Page Views

blog counter
Copyright © gskLinux | Linux Tutorials | Designed By | Gnanasekhar