My question is similar to this one. Using aliases with nohup

I took a lot of time customizing a function that I included in my .bashrc I'd like it to run with nohup, because I'd like to run a command several times in this fashion.

for i in `cat mylist`; do nohup myfunction $i 'mycommand' & done

Any tips?

有帮助吗?

解决方案 2

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

其他提示

You can do this with functions (not aliases) by nohuping a bash -c (which is essentially the same as running an external bash script).

In order for this to work, you need to mark your function as exported:

# define the function
echo_args() {
  printf '<%s> ' "$@"
  printf "\n"
}
# mark it as exported
declare -fx echo_args

# run it with nohup
nohup bash -c 'echo_args "$@"' bash_ "an argument" "another argument"

The argument bash_ to nohup provides a "name" for the bash -c subshell; that is, it becomes the value of $0 in the subshell. It will be prepended to error messages (if any), so I try to use something meaningful.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top