Thursday, 6 October 2011

How to change Crontab Editor and Add Cron Jobs to Server

Most of the time newbie install the cron and accept the terminal editor sed for crontab, which brings them towards difficulties.
In this Tutorial we have shown that how to change the Crontab Editor from sed(default Crontab Editor) to your desired one, like VIM,GEDIT,NANO or VI.

Follow the Procedure given bellow you will be able to add jobs to cron successfully.

login to your server through ssh Press(Alt+Ctrl+T) open terminal
root@pfserver-desktop1# ssh username@servername.com ( you can use IP instead of servername.com)
passwrod:

root@pfserver-desktop$
checkout your cron is installed
root@pfserver-desktop# ps -aux
: if no entry is there for cron then

root@pfserver-desktop# /etc/init.d/cron start or service cron start
started

now type the command on your terminal

root@pfserver-desktop# env EDITOR=vim crontab -e
Enter
It will open a file of crontab for you where you can add your jobs.

Now we want to add some jobs to our crontab file.

1: First you have to write a shell script or txt file and save it on your server in a known location

2: shell scrip is saved with the name of file.sh

3:root@pfserver-desktop# chmod +x file.sh
to operate as script file

4: After creating a file test it manually wither it is running correctly or not.
like i have created srag.sh file in /bin directory so i will check it manually

5: root@pfserver-desktop#/bin/sarg.sh daily

The output is :
SARG - Daily / Weekly / Monthly - Squid proxy usage reports creation tool
Written by Imran Ullah
Version: 20050202
Usage: /bin/sarg.sh [OPTIONS]
Allowed options:
manual, Create Manual report
today, Create Today report
daily, Create Daily report
weekly, Create Weekly report
montly, Create Monthly report
..............................................
SARG: No records found 20039, reading: 99.81%
SARG: End
SARG: Records in file: 20039, reading: 100.00%

Now i am adding this script to crontab file. Where in the above command the daily is used as an argument to run the script.

6:root@pfserver-desktop# crontab -l
no job for root user

7: root@pfserver-desktop# env EDITOR=vim crontab -e

# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezone.
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command

8: Press i for insert and type the following command

01 20 * * * /bin/sarg.sh daily
10 20 * * 1 /bin/sarg.sh weekly
20 20 29 * * /bin/sarg.sh monthly

Save and Exit!

9: Check out your crontab

crontab -l
This will show you the jobs you added to crontab file.

10: First line in the jobs added to crontab file the figure 01 means every minutes, 20 means 8:00 P.M, and the * means all the times, So this script will be executed every day, Each month and in Every week at 8:01 Evening, And the out put directory is defined in the sarg.sh file.

11: In second line the 1 is used for Monday (week: Sunday =0..6) so on every Monday at 8:01 P.M This script will run and produce the out put.

12: In third line 29 means Date of the Month. This will create out put on Every 29th of the month at 8:01 PM at any day of the week every year.

12: restart the service of cron for confirmation that there is no error in the new posted jobs.

service cron restart
restart OK

now can ENJOY the a cup of TEA at 08:01 * * *! Have a nice day.

May this tutorial will be helpful for you!
your comments will appreciated for further discussion!

No comments:

Post a Comment