Linux: Init Script for FSniper

As refer to my previous post on how to install fsniper, we can see that fsniper can be run on daemon mode. We can take this advantages by generate an init script to ease up the stop and start process. By default, you need to kill manually the PID and also start manually using following command:

$ fsniper --daemon

Since I am using fsniper quite a lot in order to monitor file changes on the server, I have create simple init script for fsniper. Lets create it!

1. Create the init script in init.d directory:

$ touch /etc/init.d/fsniper

2. Using your favourite text editor, open the file /etc/init.d/fsniper and paste following code:

export HOME=/root
 
case "$1" in
start)
echo -n "Starting Fsniper: "
/usr/local/bin/fsniper --daemon
echo -e "... [ \e[00;32mOK\e[00m ]"
;;
stop)
echo -n "Shutdown Fsniper: "
kill -9 `ps aux | grep "fsniper --daemon" | grep -v grep | awk {'print $2'}`
echo -e "... [ \e[00;32mOK\e[00m ]"
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: `basename $0` start|stop|restart"
exit 1
esac
 
exit 0

3. Change the permission to executable:

$ chmod 755 /etc/init.d/fsniper

Done! You can now use following command to start fsniper:

$ service fsniper start
$ /etc/init.d/fsniper start