Skip to content

Instantly share code, notes, and snippets.

@avaidyam
Created December 27, 2016 02:22
Show Gist options
  • Save avaidyam/71acb64c542ed471fe2ce834d752979a to your computer and use it in GitHub Desktop.
Save avaidyam/71acb64c542ed471fe2ce834d752979a to your computer and use it in GitHub Desktop.
Daemonize any tool!
#!/bin/bash
# modify this command to select what tool to daemonize.
CMD=top
# daemonizer:
case "$1" in
start) # start daemon
if [ -e "daemon.lock" ];
# daemon has already been started!
then echo "daemon has already been started!";
else
nohup $CMD >> daemon.log 2>&1 &
echo $! > daemon.lock
echo "daemon started"
fi
;;
stop) # stop daemon
if [ -e "daemon.lock" ]; then
if ps -p `cat daemon.lock` > /dev/null; then
kill -9 `cat daemon.lock`
echo "daemon stopped"
# daemon was not running!
else echo "daemon was not running!"; fi
rm daemon.lock
# daemon was never started!
else echo "daemon was never started!"; fi
;;
*) # print usage
echo "usage: ./daemon.sh [start|stop] [command]"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment