1

I want to have a bash command running in the background and being watched by monit. To get the PID of this process, I found this script on the web and I modified it to fit my commands: (named sdr.sh)

#!/bin/bash

NAME="sdr"
LOG_FILE="/tmp/$NAME.log"
PID_FILE="/var/run/$NAME.pid"
CMD="/usr/local/bin/rtl_fm -o 4 -A lut -s 22050 -f 254.2355M - | /usr/local/bin/multimon-ng -t raw -f alpha /dev/stdin |& /home/me/import.rb"

function startnotification {
VAR=`ps -ef | grep "$CMD" | grep -v grep | wc -l`
if [ $VAR -gt 0 ]; then
echo "$NAME already running..."
else
nohup $CMD > $LOG_FILE 2>&1 &
echo $! > $PID_FILE
echo "$NAME listener is started..."
fi
}

function stopnotification {
kill `cat /var/run/$NAME.pid`
rm -f $PID_FILE
echo "$NAME listener stopped."
}
case $1 in
start) startnotification;;
stop)  stopnotification;;
restart)
stopnotification
startnotification;;
*)
echo "usage: $NAME {start|stop}" ;;
esac
exit 0

When I execute the command

/usr/local/bin/rtl_fm -o 4 -A lut -s 22050 -f 254.2355M - | /usr/local/bin/multimon-ng -t raw -a POCSAG1200 -f alpha /dev/stdin |& /home/me/import.rb

In my bash cmd it works fine. But when I do a sdr.sh start I get the following error:

/usr/local/bin/rtl_fm: invalid option -- 'a'

As you can see, the -a is a parameter of multimon-ng and not of rtl_fm. It seems, that this pipe is not interpreted as pipe.

  • "Obverse" or "observe"? I know the word "obverse" exists. In cases like this my English is not that good to be 100% sure that you made a typo. – SPRBRN May 15 '14 at 10:10
  • Sorry, I was talking about observing, it was a typo – Racer May 15 '14 at 10:16
1

I would suspect there is some additional info being returned by that ps ... grep line in your startnotification() function. To debug this further I would run the script with some debugging turned on first.

function startnotification {
set -x
VAR=`ps -ef | grep "$CMD" | grep -v grep | wc -l`
if [ $VAR -gt 0 ]; then
echo "$NAME already running..."
else
nohup $CMD > $LOG_FILE 2>&1 &
echo $! > $PID_FILE
echo "$NAME listener is started..."
fi
set +x
}

Using set -x will enable verbose messaging, and set +x will disable it. This should shed additional light on where your script is failing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.