Skip to content

Instantly share code, notes, and snippets.

@oostendo
Created November 7, 2016 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oostendo/b22a57aacc9439f80f74b0545d38148d to your computer and use it in GitHub Desktop.
Save oostendo/b22a57aacc9439f80f74b0545d38148d to your computer and use it in GitHub Desktop.
#!/bin/sh
# start/stop service and create pid file for a non-daemon app
# from https://blog.sleeplessbeastie.eu/2014/11/04/how-to-monitor-background-process-using-monit/
# service command
service_cmd="nice -n -19 fluidsynth -is --audio-driver=alsa /usr/share/sounds/sf2/FluidR3_GM.sf2"
# pid file location
service_pid="/var/run/fluidsynth.pid"
if [ $# -eq 1 -a "$1" = "start" ]; then
if [ -f "$service_pid" ]; then
kill -0 $(cat $service_pid) 2>/dev/null # check pid
if [ $? -eq 0 ]; then
exit 2; # process is running, exit
else
unlink $service_pid # process is not running
# remove stale pid file
fi
fi
# start service in background and store process pid
$service_cmd >/dev/null &
echo $! > $service_pid
sleep 10
aconnect 20:0 128:0
elif [ $# -eq 1 -a "$1" = "stop" -a -f "$service_pid" ]; then
kill -0 $(cat $service_pid) 2>/dev/null # check pid
if [ $? -eq 0 ]; then
kill $(cat $service_pid) # kill process if it is running
fi
unlink $service_pid
aconnect -x
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment