Thursday, October 15, 2015

Anacron



Anacron stands for anachronistic.

One of cron's biggest weaknesses is that it assumes that your server or computer is always on. If your machine is off and you have a task scheduled during that time, the task will never run.

This is a serious problem with systems that cannot be guaranteed to be on at any given time. Due to this scenario, a tool called "anacron" was developed. 

#vi /etc/anacrontab


The first column specifies how often the command should be run.

The second column is the delay to use before executing the commands.

For example, the first line runs every day, five minutes after anacron is called:

1       5       cron.daily       nice run-parts --report /etc/cron.daily

The following line is run weekly (every 7 days), ten minutes after anacron is called:

7       10      cron.weekly      nice run-parts --report /etc/cron.weekly

The third column contains the name that the job will be known as in the anacron's messages and log files. The fourth field is the actual command that is run.

Friday, October 9, 2015

Using "at" jobs




Using at



            The “at” command is used to schedule a one-time task at a specific time.

            Suppose you would like your computer to rebuild your Linux kernel this evening while you're at the pub. You might proceed like this:

$ at 20.30
warning: commands will be executed using /bin/sh
at> cd /usr/src/linux
at> make all
at> <EOT>
job 2 at Wed Feb 14 19:30:00 2007

As you'll see from this, at collects the commands you want to run from its standard input, up to an EOT character (^D by default).

Your list of queued jobs can be examined using atq:

$ atq
2 Wed Feb 14 19:30:00 2007 a chris
$

Controlling access “at”
/etc/at.deny

“at” utility command

atd  –    run jobs queued for later execution 
atq  –    list queue
atrm –   delete the job 
atrun-   run jobs queued for later execution

The job run immediately 
 
[root@server1 Desktop]# at now
at> uptime >> /root/Desktop/test.txt
at>
job 4 at 2012-10-26 07:53

[root@server1 Desktop]# cat test.txt
07:54:09 up  1:24,  2 users,  load average: 0.00, 0.00, 0.00

The job run 1 minutes lately current time

[root@server1 Desktop]# at now + 1 minutes
at> uptime > /root/Desktop/test.txt
at>
job 6 at 2012-10-26 07:59

[root@server1 Desktop]# atq
6          2012-10-26 07:59 a root

[root@server1 Desktop]# cat test.txt
 07:57:00 up  1:27,  2 users,  load average: 0.00, 0.00, 0.00

You can delete a job from the queue using atrm:
$ atrm 2

Cron Job



Cron Job Basics

            The Cron program is a deamon that executes scheduled commands.It “wakes up” every minute and check the configuration files in the /var/spool/cron and /etc/cron.d directories and the crontab file, and executes commands.

          The are two types of cron jobs
                                         i.    System cron jobs
                                        ii.    User cron jobs

       System cron jobs are run as root and perform system-wide maintence tasks.
Ordinary user can create user cron jobs, which might run some user program on a regular basis.

Crontab Format

 The crontab file consists of five fields.

    Min =(0-59)
    Hour=(0-23)
      Day of the month =(1-31)
    Month =(1-12)
      Day of the week =(0-7)
      Command = 

The command above runs 59 minutes after the hour 23(11:59 PM) .The asterisks(*) , instructing cron to run the command on every day of the month, month, and day of the week.

Schedule a Job For More Than One Instance (e.g. Twice a Day)

The following script take a incremental backup twice a day every day.

This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.


00 11,16 * * * /home/fabien/bin/incremental-backup
  • 00 – 0th Minute (Top of the hour)
  • 11,16 – 11 AM and 4 PM
  • * – Every day
  • * – Every month
  • * – Every day of the week
Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours

This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/fabien/bin/database
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • * – Every day of the week
Cron Job every weekday during working hours

This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.

00 09-18 * * 1-5 /home/fabien/bin/database
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)
 System Wide Cron Schedule

System administrator can use predefine cron directory as shown below.

In the /etc directory you will probably find some sub directories called 'cron.hourly', 'cron.daily', 'cron.weekly' and 'cron.monthly'. If you place a script into one of those directories it will be run either hourly, daily,weekly or monthly, depending on the name of the directory.

  1. /etc/cron.d
  2. /etc/cron.daily
  3. /etc/cron.hourly
  4. /etc/cron.monthly
  5. /etc/cron.weekly

You need to create the script and keep it in this folder
# chmod +x /etc/cron.daily/clean.cache

Special Strings for Common Schedule

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Instead of the first five fields, one of eight special strings may appear:
string         meaning
------         -------
@reboot        Run once, at startup.

@yearly        Run once a year, "0 0 1 1 *".

@annually      (same as @yearly)

@monthly       Run once a month, "0 0 1 * *".

@weekly        Run once a week, "0 0 * * 0".

@daily         Run once a day, "0 0 * * *".

@midnight      (same as @daily)

@hourly        Run once an hour, "0 * * * *".

Schedule a Background Job Every Day using @daily

Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell scriptat 00:00 on every day.

@daily timestamp is similar to “0 0 * * *”.

@daily /home/fabien/arch-linux/bin/cleanup-logs "day started"
Or
@daily command && command

In the above ex: command 1 and 2 runs daily


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

@yearly timestamp is similar to “0 0 1 1 *”. It will execute task on first minute of every year, It may usefull to send new year greetings :)

@yearly /scripts/script.sh

 
How to View Crontab Entries?
crontab -l

crontab -u fabien –l

How to Edit Crontab Entries?
crontab -e


How do I disable email output?
0 3 * * * /root/backup.sh >/dev/null 2>&1

To mail output to particular email account let us say Fabien@yahoo.com in you need to define MAILTO variable as follows:
MAILTO="fabien@yahoo.com"
0 3 * * * /root/backup.sh >/dev/null 2>&1
 
Sample cron job
Open a text editor
Create a edit the file called “ipinfo” in home directory
Shell=/bin/bash
MAILTO=username
00 12 * * * /sbin/ifconfig

Open the terminal and type crontab ipinfo