Friday, October 9, 2015

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

Wednesday, October 7, 2015

login shell vs interactive shell




System Wide settings  

              A shell is a shell is a shell -- e.g., a login bash shell is the same program (like /bin/bash) as a nonlogin bash shell. The difference is in the way that the shell acts:


            In a Login shell you are prompt for your Login username and password

           when you first log in to a Unix system from a terminal, the system normally starts a login shell. The login shell uses a collection of startup files to help create an environment such as environment variables, search path, and subshells.

           The files in the /etc directory generally provide global settings. If an equivalent file exists in your home directory it may override the global settings.


Login shell process:

Frist the bash login shell looks for the global settings.

It first initiates the  

/etc/profile.

The /etc/profile file provides the system wide default environment variables. Typically this sets up the umask, LOGNAME, and mail directories etc. It can also be used to change the default command search path (PATH) for all users on the system.

The /etc/profile.d Directory 

This file is actually a line in the /etc/profile. It loads are the file in the  /etc/profile.d directory, where the individual initialization scripts are placed: 

/etc/profile.d/* 

Any thing you need to modify,you don’t want to create in /etc/profile,you can create it in .sh and put it in the /etc/profile.d/ director.


So if a system updates occurred all the changes you have written in the /etc/profile got changes .but if you place files in the /etc/profile.d nothing happens to your executable  files.


/etc/bash.bashrc 

It contains the System wide aliases and functions.Personal aliases and functions should go into ~/.bashrc.it Provides colored /bin/ls and /bin/grep commands.Used in conjunction with code in /etc/profile.

These three complete the system wide settings

User wide settings

IT looks for three profile

/home/user/.bash_profile
/home/user/.bash.login
/home/user /profile

/home/user/.bashrc

This file is where you put your history length,Xterm color and alias
 
   


Here you can see ll commands returns ls –aLF

Where do I get all these ?
It all there in the /etc/skel

.bash_logout
What it does when the login shell exits it clears the console history

 Interactive shell is when you are already logged in GUI mode and open a terminal, and then you can tell that it is an interactive shell