Friday, February 11, 2011

Learning Crontab (crontab instant tutorial)


In this article I am going to discus one of the most promising feature of *nix systems.

1. What is crontab


crontab is the functionality offered by almost all *nix systems, which gives us the ease to automate any action on the machine at specific time frame.

2. How the crontab looks


crontab looks like five * marks following by a specific command. these five * marks do carries different meaning.

$ crontal -l

* * * * * /usr/home/sudarshan/myScript.sh

3. Defination of five * marks


These five * marks in the beginning of every cron entry do have following meaning -

* - Minute when the cron will execute (* means every minute)
* - hour when the cron will execute (* means every hour)
* - date when the cron will execute (* means every day of a month)
* - Month when the cron will execute (* means every month)
* - day of week when the cron will execute (* means every day of week)

Examples -

*/5 * * * * script (script will be executed every five minutes)
5 10 * * * script (script will be executed everyday at 10:05 AM)
15 18 * * WED script (script will be executed every wednesday at 6:15 PM)
5 11 3 * * script (script will be executed on 3rd of every month at 11:05 AM)
5 13 5 6 * script (script will be executed on 5th June at 1:05 PM)


4. How to view/edit crontab entry


crontab entries can be viewed/edited by the help of following commands -

crontab -l (list down all the crontab entries for current user)
crontab -u username -l (list down all the crontab entries for a specific user*)
crontab -r (removes crontab entries for current user)
crontab -u username -r (removes crontab entries for a specific user*)
crontab -e (edit crontab entries for current user)
crontab -u username -e (edit crontab entries for a specific user)

* -- for another user specific commands you need to be root

5. Best practices when setting a crontab


It is always better to set crontab entries from a external file. Benefits of this are.. if someone or you by mistake removes the crontab entries then you don't have to remember all the entries. You simply need to call the same file again and you will get your cron entries back.
personally this happened with me as I had more then 25 cron jobs in my crontab and suddenly I lost all of them, as by mistake I hit 'crontab -r' command.
So to keep the sanity it is advised to set the cron jobs from a file.
you can write all from cron jobs in any file and set the file in your crontab.. make sure that you copy all the previous cron jobs and incorporate them in your file.. as by using the file you will loose your initial cron job entries.

command to do the same -

crontab /path/to/the/file/crons.txt

6. Setting PATH in crontab


there can be a situation that you will be having multiple scripts in a directory which you want to call in cronjob. For this you don't have to mention the entire path every time.
Simply the pach then the entry -

PATH=/path/to/scripts/
* * * * * script1.sh
* * * * * script2.sh
* * * * * script3.sh

7. Keywords in crontab

This is quite interesting. crontab do support miltiple keywords for entries -
@yearly script (this will execute the script file at 00:00:00 Hrs on 1st Jan of every year)
@monthly script (script execution on 1st minute of every month)
@daily script (script execution on first minute of everyday)
@reboot script (script will be executed everytime when machine is rebooted)


Have fun with one of the most interesting part of *nix systems.

Thursday, January 27, 2011

Mastering Vi Editor in a simple way


Following tutorial will guide you through mastering Vi Editor in the simplest possible way -

1. Opening a file in Vi Editor -
vi <file name>
vi -r <file name> [useful if the file was not saved earlier because of sudden connection failure]

2. Quit and save a file in vi editor -
:q [wuit without saving]
:wq or :x [quit after saving the changes]
        :w <file name> [to save a file with new name]

3. Traversing a file in vi -
k or <up arrow> [to go up]
j or <down arrow> [to go down]
h or <left arrow> [to go left]
l or <right arrow> [to go right]

4. editing in vi editor -
i [replace charactor after cursor untill esc is pressed]
a [replace charactor before cursor untill esc is pressed]
r [replace the charactor where cursor is right now]
R [replace the chars starting the cursor position, untill esc is presses]
x [delete the charactor where cursor is right now]
dd [delete entire current line]
ndd [n is a number, delete n following lines in cluding current]
D [delete the line after cursor]
o [place a new line after current line to enter text untill esc is pressed]
O [place a new line before current line to enter text untill esc is pressed]
C [change the char in current line untill esc is pressed]
cc [ehcnge entire line]
yy [copy current line into buffer]
p [paste the text from buffer]
u [undo, but effective till one last change only]

5. Search and Replace in vi editor -
/String [search String forward]
?String [search String backward]
:%s/term1/term2/g [replace term1 with term2 in entire file]

Hope this will help you with vi editor.

Script to find file without a specific String


Sometimes we land in a situation where we need to find all files in the system which does not contain a specific String.
I wrote a simple script to find a file without the string.

As a demonstration I used the file type as xml. You can customize it as per your requirement.
-----------------------------------------------------------
#!/bin/bash
typeset -i mRep
for mFile in `find . -name "*.xml"`
do
  mRep=`egrep -c 'string' $mFile`
  if [ $mRep -eq 0 ]; then
        echo "File = "$mFile" does not contain 'string'"
  fi
done
-----------------------------------------------------------
Feel free to write back if you have issues in implementing the same.

Interesting Unix Commands -1


Here are a couple of interesting Unix commands. That Makes the life easier for a Unix / Linux Administrator.
I will keep posting on this series.

1. List Down all the users in the system
cat /etc/passwd | sed -e 's/:.*$//' (Prints usernames)
cat /etc/passwd | cut -d : -f1,5 (prints usernames and description of the user)

2. Command to see all the processes with the child processes.
ptree -a

3. Following command is very interesting. This command will tell the entire history of the user. If the session is connected, when the session started (Login details), mail details, further meeting plans and all.
   This command is very sensative in nature as this gives the entire data of all the users logged in, Please use this command wisely.
cat /etc/passwd | cut -d : -f1 | xargs finger {}\;


Tuesday, January 18, 2011

Command to display tree structure in Solaris

Following combination of fond and sed with a rich use of regex will display tree structure of directories in solaris machines.

find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'