3

My problem is that I have an init script that is generating thousands of defunct bash processes. My goal is to have a program called "fr24feed" in its own screen session on boot. After looking at examples on the web I've written this init script.

PROG="fr24feed"
PROG_PATH="/home/pi/fr24feed"
PROG_ARGS="--fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003"
PIDFILE="/var/run/fr24feed.pid"

start() {
  if [ -e $PIDFILE ]; then
      ## Program is running, exit with error.
      echo "Error! $PROG is currently running!" 1>&2
      echo "Already running"
      exit 1
  else
      echo "Starting"
      cd $PROG_PATH
      sleep 2
      sudo -u pi screen -S fr24feed -d -m ./$PROG $PROG_ARGS 2>&1 > /dev/null &
      echo "$PROG started"
      touch $PIDFILE
  fi
}

stop() {
  if [ -e $PIDFILE ]; then
      ## Program is running, so stop it
     echo "$PROG is running"
     killall $PROG
     rm -f $PIDFILE
     echo "$PROG stopped"
  else
      ## Program is not running, exit with error.
      echo "Error! $PROG not started!" 1>&2
      exit 1
  fi
 }

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root" 1>&2
  echo "This script must be run as root" 1>&2 >> /home/pi/fr24feed.log
  exit 1
fi

case "$1" in
  start)
      start
      exit 0
  ;;
  stop)
      stop
      exit 0
  ;;
  reload|restart|force-reload)
      stop
      start
      exit 0
  ;;
  **)
      echo "Usage: $0 {start|stop|reload}" 1>&2
      exit 1
  ;;
esac
exit 0

Using ps -ax I see

2063 ?        Ss     0:19 SCREEN -S fr24feed -d -m ./fr24feed --fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003
2064 pts/4    Ssl+  49:19 ./fr24feed --fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003

Followed later by thousands of entries like

3073 pts/4    Z+     0:00 [bash] <defunct>
3078 pts/4    Z+     0:00 [bash] <defunct>
3083 pts/4    Z+     0:00 [bash] <defunct>
3088 pts/4    Z+     0:00 [bash] <defunct>
3092 pts/4    Z+     0:00 [bash] <defunct>

Since they're all on pts/4 I suspect they're being generated by my init script but don't see where I've gone wrong. Perhaps STDERR and STDOUT are not being correctly redirected to /dev/null?

  • I don't know if this is it, but your redirection should look like: >/dev/null 2>&1 - also, you want to put the whole thing in a subshell, and try to sudo in any scripted backgrounded processes - that's crazy. But maybe ( cmd >&2 ) 2>&- & – mikeserv Apr 2 '14 at 6:15
  • @mikeserv - I tried >dev/null 2>&1 but still got the defunct processes. I wasn't able to follow the rest of your comment. – pgcudahy Apr 3 '14 at 22:19
  • Did you ever figure this one out? – mikeserv Apr 7 '14 at 8:27
  • @mikeserv Nope, I've started a list of at least 10 different variations, all with no success. – pgcudahy Apr 12 '14 at 16:18
1

Perhaps STDERR and STDOUT are not being correctly redirected to /dev/null?

Correct. What 2>&1>/dev/null does is redirect 2 to the same place as 1, which is the controlling (pseudo) terminal, and redirects 1 to /dev/null:

» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 
Testing stdout
Testing stderr

» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 1> /dev/null
Testing stderr

» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 2> /dev/null
Testing stdout

» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 2>&1 > /dev/null
Testing stderr

The simple way to do this is to just use &>:

» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' &> /dev/null
[no output]

See example 3.6 here. Note that although it says this redirects "every output", this refers to the executing shell, so if the executed process uses other file descriptors for logging, or redirects its own stdout/stderr so they never reach the shell, those are not affected.

  • Tried both 'sudo -u pi screen -S fr24feed -d -m ./$PROG $PROG_ARGS &> /dev/null' As well as 'sudo -u pi screen -S fr24feed -d -m ./$PROG $PROG_ARGS &> /dev/null &' But the defunct processes kept coming. – pgcudahy Apr 3 '14 at 22:22

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.