Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function using nohup

I am trying to call a function using nohup like this:

function1(){
    while true 
    do
        echo "function1"
        sleep 1
    done
}

nohup function1 & 
# ...... some other code

but may be the function isn't seen by nohup and I get this error:

nohup: failed to run command `function1' : No such file or dictionary

I don't want to create new sh file for my function. How can I fix this?

like image 547
sajad Avatar asked May 08 '13 08:05

sajad


People also ask

What is the difference between nohup and &?

nohup catches the hangup signal (see man 7 signal ) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP at all). Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal ( kill -SIGHUP <pid> ).

How does nohup command work?

Nohup, short for no hang up is a command in Linux systems that keep processes running even after exiting the shell or terminal. Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal.

How do I redirect nohup output?

If you do not specify the output file, nohup redirects the output to a nohup. out file. You can override this by using the redirection symbols. Adding the file descriptors 1 and 2 enables standard output, and standard error redirects to the ping.


4 Answers

Another solution:

function background {     echo TEST } export -f background   nohup bash -c background & 
like image 118
user7813184 Avatar answered Sep 30 '22 01:09

user7813184


nohup applies to commands and not to script functions.

For example, the script (say func.sh) that contains function1() should call the function-:

function1(){     while true      do         echo "function1"         sleep 1     done  }  function1 

Now call the script func.sh with nohup in the background-:

nohup ./func.sh & 

If you need to disable the hangup signal from within the script use the shell built-in trap. The example ignores SIGHUP but can be used to ignore others (e.g. SIGINT).

trap "" HUP   # script will ignore HANGUP signal 
like image 44
suspectus Avatar answered Sep 30 '22 01:09

suspectus


I find a working solution for me - define the function in a file (e.g. .functions) then run the function with nohup:

nohup bash -c "source .functions; function1" &

Tested on Ubuntu 13.04.

like image 22
Lachezar Avatar answered Sep 30 '22 01:09

Lachezar


Since nohup must be supplied with a filename not a function as a workaround this is what can be done:

function1(){
    while true 
    do
        echo "function1"
        sleep 1
    done

}

echo "$@" | grep -q -- "--nohup" && function1 || nohup $0 "$@" --nohup & 

So when this script gets called with the current arguments:

  • `echo "$@" | grep -q -- "--nohup" will return an error status so
  • nohup $0 "$@" --nohup & will be invoked, which will invoke this script passing the current arguments and a new argument --nohup

And when this script is called with argument --nohup

  • `echo "$@" | grep -q -- "--nohup" will return with zero status (success) so
  • function1 will be invoked
like image 44
Adam Siemion Avatar answered Sep 30 '22 02:09

Adam Siemion