sajad
sajad

Reputation: 2174

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?

Upvotes: 29

Views: 29913

Answers (8)

reitssu
reitssu

Reputation: 1

try edit nohup function1 & to this

function1=$(function1)
nohup $function1 --nohup &

Upvotes: 0

Anciaux Frank
Anciaux Frank

Reputation: 11

About:

function background {
     echo TEST 
} 
export -f background 

nohup bash -c background &

For people encountering the issue:

export: -f: unknown option – 

Be sure to be in bash or csh, because "export -f" is usually not recognized in e.g. ksh

Sorry, not yet the reputation to comment the IceTea answer directly.

Upvotes: 1

James Carey
James Carey

Reputation: 91

Instead of using nohup, which is tailored for files, you can implement the same result like this:

(trap '' HUP INT
 while true
 do
   echo "function1"
   sleep 1
 done
) </dev/null 2>&1 1>nohup.out &

As I tend to launch these processes from a parent script and the parent script may do other work if the parent process is interrupted I want the child process to ignore the INT signal and keep on running.

Upvotes: 9

user7813184
user7813184

Reputation: 351

Another solution:

function background {
    echo TEST
}
export -f background 

nohup bash -c background &

Upvotes: 35

Lachezar
Lachezar

Reputation: 6703

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.

Upvotes: 8

scavenger
scavenger

Reputation: 408

Yes ! It is possible however tricky, and strictly bash > v2 compatible :

function1(){ local msg=${*:-function1}; echo msg=$msg; }
nohup -- sh -c "$(typeset -f function1); function1 MESSAGE" >nohup.log 2>&1 0</dev/null &

And don't forget "typeset" is bash deprecated in favor of "declare" (though I don't entirely agree with this).

Upvotes: 3

suspectus
suspectus

Reputation: 17258

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

Upvotes: 10

Adam Siemion
Adam Siemion

Reputation: 16039

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

Upvotes: 6

Related Questions