Thursday, June 30, 2016

[Quick Tips: Cron Jobs] scheduling cronjobs with multiple examples

scheduling cronjobs with multiple examples


The software utility Cron will help in scheduling cronjobs for maintenance activities, automating regular boring stuff by scheduling shell scripts.  We can also achieve regular periodic checks. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.
cronjob scheduling is possible using crontab command and each and every user crontab is separate
# crontab -e   = To edit cronjob schedule
# crontab -l = To list scheduled cronjobs
# crontab -r = To remove all cronjobs in single attempt
whenever you schedule a cronjob with particular user the same cronjob will run using same user, ensure user is having execution permission to the user.

Know crontab fields and its values

[root@server ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

1. Scheduling cronjobs to run @every minute

To run cronjob yet every one minute first field is minutes, first field we can right with separated by comma. below three examples we can use to schedule a cronjob to run every minute.
# crontab -e
* * * * * sh /scripts/everyminute.sh
1,2,3,4,5,6,7,8,9,10......60 * * * * sh /scripts/everyminute.sh
*/1 * * * * sh /scripts/everyoneminute.sh

2. Scheduling cronjobs to run @every 5 minutes

below two examples to schedule cronjob for every 5 minutes, again we have to make use of first field to run script @every 5 minutes because first field is minutes
*/5 * * * * sh /scripts/testscript.sh
5,10,15,20,25,30,35,40,45,50,55,0 * * * * sh /scripts/testscript.sh

3. Scheduling cronjobs to run @every 30 minutes

if we want monitoring our file system @every 30 minutes using shell script you can do using below
*/30 * * * * sh /scripts/FS-Monitoring.sh
OR
0,30 * * * * sh /scripts/FS-Monitoring.sh

4. Scheduling cronjobs to run @every hour

To schedule a cronjob to run @every hour we can make use of first field either second field.
0 * * * * sh /scripts/everyhour.sh

OR we can also make use of special schedule
@hourly sh /scripts/everyhour.sh

5. Scheduling cronjobs to run @Every 3 Hours OR 5 Hours

below examples for every 3 hours schedule
0 */3 * * * sh /scripts/cronjob-every-3hours.sh
OR
* 0,3,6,9,12,15,18,21 * * * sh /scripts/cronjob-every-3hours.sh

6. Scheduling cronjobs to run @every day Or once in a day

we would like to schedule an cronjob to send daily reports once in a day. examples are below
59 23 * * * sh /scripts/daily-report.sh
OR
we can also make use of special schedule
@daily sh /scripts/daily-report.sh

7. Schedule cronjobs to run every alternate day 

Every alternate day means in between three days middle day job will not run
0 * * * 0,2,4,6 sh /scripts/every-alternate-day.sh

8. Run cronjob first and Second Saturday of the month

it means that cronjob should run first Saturday of the month and Second Saturday of the month, remaining Saturdays cron should not run. Detailed explanation of below example is yet any cose first Saturday will fall from 1-7 it means range from 1st date to 7th date. Second Saturday may fall from 15th to 21st Dates.
0 1 1-7,15-21 6 /scripts/every-first-second-sat.sh

9. Cronjob for every week

to schedule a cronjob to run @every week we can make use of week of the day 5th field in crontab
0 0 * * 0 sh /scripts/every-week.sh
OR
we can also make use of special schedule
@weekly sh /scripts/every-week.sh

10. Run cronjob @every month 1st date

Here we have to use day of the month, field 3
0 0 1 * * sh /scripts/every-mont-1st.sh
OR
@monthly sh /scripts/every-month.sh

11. Schedule cronjob to run once in a year

I would like to schedule an cronjob to say happy new year to all the employees on midnight of every year 31st Dec.
59 23 31 12 *  echo "Happy New Year to All"
OR
@yearly echo "Happy New Year"

12. I want to run a cronjob @every server reboot

every time whenever server is rebooted i would like to get notification. We can make use of special schedule
@reboot echo Server Rebooted | mail -s "Server Rebooted `hostname`" 
That’s all about scheduling cronjobs in different timings.

Tuesday, June 28, 2016

[Quick Install: Root Cern]: Installing Root 6.04/16 on Ubuntu 15.10

Installing Root 6.04/16 on Ubuntu 15.10

The overview consists of the following sections:
Preparing Your System
Downloading, Building and Installing ROOT
Testing ROOT
Quitting ROOT

Preparing your System

You need to check the prerequisites, though, and make sure the necessary packages are installed. A few packages seem to have changed in name, I marked them below on the list from CERN (original list is here):
Required packages:
  • git: for /usr/bin/git
  • dpkg-dev for dpkg-architecture (needed by configure to locate system libraries starting from 11)
  • make: for /usr/bin/make
  • g++: for /usr/bin/g++ (ROOT 6 requires g++ 4.8 or clang 3.4 for how to install g++ 4.8 on Ubuntu 12 see ubuntuhandbook (link is external))
  • gcc: for /usr/bin/gcc
  • binutils: for /usr/bin/ld
  • libx11-dev: for usr/include/X11/Xlib.h and for /usr/lib/libX11.so
  • libxpm-dev: for /usr/include/X11/xpm.h and for /usr/lib/libXpm.so
  • libxft-dev: for /usr/include/X11/Xft/Xft.h and for /usr/lib/libXft.so
  • libxext-dev: for /usr/include/X11/extensions/shape.h and for /usr/lib/libXext.so
  • libpng: png library (does not exist on 15.10 – use libpng-dev)
  • libjpeg: jpeg library (does not exist on 15.10 – use libjpeg-dev)
  • python: (ROOT6 requires version >= 2.7)
Most common optional packages:
  • gfortran: for /usr/bin/gfortran
  • openssl-dev or libssl-dev: for /usr/include/openssl/pem.h and /usr/lib/libssl.so and /usr/lib/libcrypto.so (oppenssl-dev does not exist on 15.10 use libssl-dev)
  • libpcre3-dev: for /usr/bin/pcre-config
  • xlibmesa-glu-dev: for /usr/include/GL/gl.h and for /usr/lib/libGL.so
  • libglew1.5-dev: for /usr/include/GL/glew.h and for /usr/lib/libGLEW.so
  • libftgl-dev: for /usr/bin/pkg-config
  • libmysqlclient-dev: for /usr/bin/mysql_config
  • libfftw3-dev: for /usr/include/fftw3.h and for /usr/lib/libfftw3.so
  • cfitsio-dev: for /usr/include/fitsio2.h and for /usr/lib/libcfitsio.so (does not exist on Ubuntu 15.10 use libcfitsio-dev)
  • graphviz-dev: for /usr/include/graphviz/gvc.h and for /usr/lib/libgvc.so
  • libavahi-compat-libdnssd-dev: for /usr/include/dns_sd.h and for /usr/lib/libdns_sd.so
  • libldap2-dev: for /usr/include/ldap.h and for /usr/lib/libldap.so
  • python-dev: for /usr/include/python2.7/Python.h and for /usr/lib/libpython2.7.so
  • libxml2-dev: for /usr/bin/xml2-config
  • libkrb5-dev: for /usr/include/krb5.h and for /usr/lib/libkrb5.so
  • libgsl0-dev: for /usr/include/gsl/gsl_version.h and for /usr/lib/libgsl.a (that’s a zero not an o at the end)
  • libqt4-dev: for /usr/include/qt4/Qt/qglobal.h and for /usr/lib/libQtCore.so

Downloading,Building and Installing ROOT

For this, you’ll need to get the source from root.cern.ch and
  • open a terminal and change directory to the Downloads directory (or the directory you want to put the downloaded file into)
  • create directory named ‘root-6.06.02’ and cd into this directory
  • download the source files with ‘wget https://root.cern.ch/download/root_v6.06.02.source.tar.gz’
  • unpack the files by typing tar -xvzf root_v6.06.02.source.tar.gz this will create a directory named ‘root_v6.06.02.source’
  • rename directory ‘root_v6.06.02.source’ to ‘source’
  • the source cannot be build inside the source directory, so we have to create another directory which we name ‘build’
  • cd into the ‘build’ directory
  • now we create the necessary files for the build process
  • if this step completed successfully, we now have lots of files and directories inside the build directory. Now we start the real build process, which will take some time. The following command will just do the build, but not install the files (the ‘.’ at the end is necessary)
If you want to build and install type the following instead of the above
The difference between the two commands is that the later performs a system wide installation of ROOT (for which you’ll need root privileges), while the first one is just for the installing user.

Testing the ROOT installation

After the build process completed successfully, i.e. showing no error messages, you can now run ROOT by typing while still in the ‘build’ directory
You now see a short welcome message and the ROOT comand prompt
Note that in case you build with ‘cmake –build . –target install, you can type
anywhere in the system to start the ROOT environment
We know perform two basic tests to see if the basics are working. First type 1+1 and hit enter. You should see (int) 2 as reply, if everything is fine:
Next, lets test the graphical environment. Type ‘TF1 f1(“func1″,”sin(x)/x”,0,10)’ hit enter and then type ‘f1.Draw()’ and hit enter as shown below:
A graphics window should now have been opens.

Quitting ROOT environment

You close the ROOT environment simply by typing ‘.q’: