Saturday, April 23, 2022

[SOLVED] Bash script runs into infinite loop. Why?

Issue

This init script shall start a service using nohup with the "start" parameter. All other parameters shall be passed as-is. (Restart is provided for convenience.)

#!/bin/sh
# Foo Startup Script

LOGFILE="/var/log/foo/foo.log"
WORKDIR="/usr/local/foo"

nohup() {
        nohup $WORKDIR/bin/foo $@ >> $LOGFILE  2>&1 &
}
other() {
        $WORKDIR/bin/foo $@
}

case "$1" in
  start)
        nohup $@
        ;;
  restart)
        other stop
        nohup start
        ;;
  *)
        other $@
        exit
esac

With "start", the script runs into an infinite loop with nohup forking more and more processes (aka. fork bomb) but why? (No output is written to the log file.)


Solution

nohup() {
    /usr/bin/nohup $WORKDIR/bin/foo "$@" >> $LOGFILE  2>&1 &
}


Answered By - Barmar
Answer Checked By - Marilyn (WPSolving Volunteer)