Call a Function Using Nohup

run a function or alias set in bashrc, or profile through nohup

nohup will not work with functions. You need to create a shell script which wraps and executes the function. The shell script then you can run with nohup

Like this:

test.sh

#!/bin/bash
function hello_world {
echo "hello $1, $2"
}

# call function
hello_world "$1" "$2"

chmod +x test.sh and then call it in your for loop:

for i in `cat mylist`; do 
nohup ./test.sh $i 'mycommand' &
done

Call a shell script method with parameter using nohup

If you pass parameters, you have to use them somehow. One possibility is technically do write your script as

start(){
first="$1";
echo "arg is $first"
}

"$@"

The last line would - in your case - expand to start arg1 and therefore call your function. However it is risky to do this: Since the first parameter to your script (i.e. start) is treated as a command to be executed, the user of the script could inject any code via the parameter. This is a serious security hole.

I would at least verify the first parameter somehow, or redesign your script completely. For instance, it is unclear why you define a function, if the script isn't doing anything else than calling this function.

Non-blocking nohup in bash function?

The nohup command still has your stdout and stderr open. Redirect to /dev/null like this:

nohup someplayer &>/dev/null &

and your function returns immediately.

Bash : how to use a function (which is a string param) in an other function

Functions are not first-class citizens. The shell knows what they are, but other commands like find, xargs, and nice do not. To call a function from another program you need to (a) export it to sub-shells, and (b) explicitly invoke a sub-shell.

export -f untar
nn bash -c 'untar test.txt.tar'

You could automate this if you want to make it easier for the caller:

nn() {
if [[ $(type -t "$1") == function ]]; then
export -f "$1"
set -- bash -c '"$@"' bash "$@"
fi

nohup nice -n 15 "$@" &
}

This line deserves an explanation:

set -- bash -c '"$@"' bash "$@"
  1. set -- changes the current function's arguments; it replaces "$@" with a new set of values.
  2. bash -c '"$@"' is the explicit subshell invocation.
  3. bash "$@" are the arguments to the subshell. bash is $0 (not used). The outer existing arguments "$@" are passed to the new bash instance as $1, $2, etc. This is how we get the subshell to execute the function call.

Let's see what happens if you call nn untar test.txt.tar. The type -t check sees that untar is a function. The function is exported. Then set changes nn's arguments from untar test.txt.tar to bash -c '"$@"' bash untar test.txt.tar.

Why is the following bash script not working when called with nohup?

Note that to declare a function in POSIX standard shell, you just write the function name followed by a pair of empty parentheses, and a body in curly brackets:

#!/bin/bash

hello() {
echo "Hello World"
}

hello

Some shells, like bash, also accept the function keyword to declare functions, but based on your error message, it looks like the shell that you are using does not accept it. For portability, it would be best to use the POSIX standard declaration style.

The reason you see the difference between running the script directly and running the script under nohup is the blank lines at the beginning of the file. The #! need to be the first two characters of the file in order to specify the interpreter, otherwise that is just a comment. It looks like when executed from within a Bash shell, the script gets interpreted by Bash and it recognizes the Bash extension. But when run from nohup, it gets interpreted by sh, which on some systems is a more bare-bones shell like dash that only supports the POSIX syntax above.

Calling a script within a script that was run via nohup

This isn't a nohup issue. You are including a source file using a relative file name. Try:

. $(dirname ${BASH_SOURCE})/functions/include_me

to include a source file located relative to ${BASH_SOURCE}

How to use `while true` with nohup?

Try

nohup bash -c 'while true; do timeout 1h python worker.py --log-level=ERROR; done'

When using nohup, the print function won't print until the entire program is finished

It's likely that output buffering is occurring. Try manually flushing standard output:

for i in 1:3
sleep(2)
print("i = ", i, "\n")
flush(stdout)
end


Related Topics



Leave a reply



Submit