Friday, 14 June 2013

Shell script to stop hadoop daemons and shutdown/restart the system in one-step.

I am working on some things related to hadoop and had it installed and running. Every time I had to shut down my lap, stopping all 5 nodes took my not-really-valuable time, but yes, important to me. A beginner in shell scripting though, I thought I'd just write an easy script to make it a one-step process. Can be used to stop any service, as well.

#!/bin/sh

sudo service hadoop-0.20-namenode stop
sudo service hadoop-0.20-datanode stop
sudo service hadoop-0.20-secondarynamenode stop
sudo service hadoop-0.20-jobtracker stop
sudo service hadoop-0.20-tasktracker stop

if test $1 = s; then 
 sudo shutdown -h now 

elif test $1 = r; then
sudo shutdown -r now 

elif test $1 = n; then
echo "Tyler was here." 

fi

#Arguments s,r,n are used to shut down, restart and do nothing respectively.

To run the script, go to terminal (move to the directory where you have made the executable file) and type in:

./script-name *argument*

Here's some explanation if you are another beginner:
$1 equals the first argument. $2 would let you give a second argument. The elif loops here compares the argument to s,r or n and performs operations as needed. 

Cheers. :)