nohup and & versus functions

when i have a function definition and function call in my script , i am unable to run my script in background with nohup..
Help me out please.....

nohup can't run functions, period. nohup is not a shell builtin, so only runs executable files.

& should work, though. You can do nohup's signal-trapping + redirection in the shell itself too.

( trap "true" HUP ; myfunction ) > nohup.out 2>/dev/null </dev/null & disown

disown may not be needed on all shells.

If it doesn't work you'll need to post your complete code, plus what your system and shell is, to figure out what's going wrong.

1 Like

would you please explain me a little more about what you said...:slight_smile:

Shell functions don't work anywhere except inside the shell. nohup is not part of the shell, so you can't feed it a function.

from man nohup you can tell that nohup does the following things:

  1. Ignore the HUP signal
  2. Redirect stdout into a file
  3. Redirect stderr into /dev/null
  4. Redirect stdin from /dev/null

All of that can be done in the shell with no help from nohup.

trap "true" HUP # Do nothing but run "true" on SIGHUP
# runs a shell function in the background, with its files redirected
# the same way nohup does.  & puts it in the background.
# 'disown' tells the shell not to wait for it on exit.
# disown isn't available in some shells, but if the shell
# doesn't have it, it also doesn't need it.
function > logfile 2> /dev/null < /dev/null & disown