0

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.)

Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63
  • 5
    Looks to me like every time you call nohup(), the first thing it does is recall nohup() (You seem to have redefined it.) Try calling it something else and see if you have the same problem. – TyrantWave Feb 12 '13 at 10:38
  • 1
    yeah, call your function `no_hup` or something other than `nohup` – Oerd Feb 12 '13 at 10:43
  • Or run `/bin/nohup`. Also, all the `$@` parameters need to be quoted `"$@"`, otherwise it's treated just like `$*`. – Barmar Feb 12 '13 at 10:50
  • On another note, this should not be fork-bomb... One process is creating just ONE process in background. for fork-bomb, it should create multiple background copies of itself... – anishsane Feb 12 '13 at 12:24

2 Answers2

1
nohup() {
    /usr/bin/nohup $WORKDIR/bin/foo "$@" >> $LOGFILE  2>&1 &
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    another way : `nohup() { command nohup ........ ; }` (that way, if find the first "nohup" command it can find in the PATH, not necessarily located in /usr/bin depending on the distrib) (and it put a terminating ";" before closing "}", as needed in newer bash) – Olivier Dulac Feb 12 '13 at 11:01
  • 1
    @OlivierDulac, +1 for "command nohup". You only need a trailing semicolon if the close brace is on the same line: newline (and ampersand) is also a command terminator. – glenn jackman Feb 12 '13 at 17:58
  • @glennjackman : +1 for the "only need ';' if closing brace on the same line" ... I can't believe I didn't knew/groked that before ^^ – Olivier Dulac Feb 13 '13 at 09:44
1

Most likely:

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

Your function nohup calls itself. The easiest solution is to give the function a different name.

If you want to be fancy, you could try either precomputing the full path to the nohup binary, or using the shell builtin version (if it exists):

builtin nohup --help || native_nohup="$(which nohup)"
nohup() {
        if test -z "$native_nohup"; then
                builtin nohup $WORKDIR/bin/foo $@ >> $LOGFILE  2>&1 &
        else
                $native_nohup $WORKDIR/bin/foo $@ >> $LOGFILE  2>&1 &
        fi
}

but I don't really see that being necessary or useful. It's much easier to just rename the function.

David Z
  • 128,184
  • 27
  • 255
  • 279