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

4
  • 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. Commented Feb 12, 2013 at 10:38
  • 1
    yeah, call your function no_hup or something other than nohup Commented Feb 12, 2013 at 10:43
  • Or run /bin/nohup. Also, all the $@ parameters need to be quoted "$@", otherwise it's treated just like $*. Commented Feb 12, 2013 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... Commented Feb 12, 2013 at 12:24

2 Answers 2

1
nohup() {
    /usr/bin/nohup $WORKDIR/bin/foo "$@" >> $LOGFILE  2>&1 &
}
Sign up to request clarification or add additional context in comments.

3 Comments

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)
@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.
@glennjackman : +1 for the "only need ';' if closing brace on the same line" ... I can't believe I didn't knew/groked that before ^^
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.