I've been working on ubuntu since many years but not quite familiar with bash scripting. My development tools involve servers like nginx and apache, the mysql database and beanstalkd. Obviously, I just can't keep all these services running always and overwhelm my CPU capacity!, so I need a way of starting a bunch of these services without having to write poetry like:
sudo service apache2 start
sudo service mysql start
..blah blah..
Hence I've written this script called kick (no laughs!), so I can just pass it some parameters and say:
kick start apache2 mysql
# OR
kick stop nginx php-fpm
Since the starting mechanism of each of these services are different, I had to deal each one in a separate if condition. I would like to know from you bash experts whether this is the correct way to do it:
filename: kick
#!/bin/bash
function docmd()
{
#echo 'func',"$1","$2"
if [ "$1" = "nginx" ];
then
if [ "$2" = "sh" ]; then
/home/prahlad/programs/nginx-1.6.0/sbin/nginx -s stop
elif [ "$2" = "re" ]; then
/home/prahlad/programs/nginx-1.6.0/sbin/nginx
sleep 1
/home/prahlad/programs/nginx-1.6.0/sbin/nginx -s stop
else
/home/prahlad/programs/nginx-1.6.0/sbin/nginx
fi
elif [ "$1" = "php-fpm" ]
then
if [ "$2" = "sh" ]; then
pkill php-fpm
elif [ "$2" = "re" ]; then
pkill php-fpm
sleep1
/home/prahlad/programs/php-5.4/sbin/php-fpm
else
/home/prahlad/programs/php-5.4/sbin/php-fpm
fi
elif [ "$1" = "apache2" ]
then
if [ "$2" = "sh" ]; then
sudo service apache2 stop
elif [ "$2" = "re" ]; then
sudo service apache2 restart
else
sudo service apache2 start
fi
elif [ "$1" = "mysql" ]
then
if [ "$2" = "sh" ]; then
sudo service mysql stop
elif [ "$2" = "re" ]; then
sudo service mysql restart
else
sudo service mysql start
fi
else
echo "Unrecognized verb",$1,$2
fi
}
cmd=""
if [ "$1" == "restart" ]; then
cmd="re"
elif [ "$1" == "stop" ]; then
cmd="sh"
elif [ "$1" == "start" ]; then
cmd="st"
fi
for name in $@
do
if [ "$name" != "start" ] && [ "$name" != "stop" ] && [ "$name" != "restart" ]; then
docmd $name $cmd
fi
done