Search code examples
shellnohupbash

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


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?


Solution

  • 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