3

I create a script, paste data in it, saving, executing, and delete:

vi ~/ms.sh && chmod +x ~/ms.sh && nohup ~/ms.sh && rm ~/ms.sh

#!/bin/bash

commands...

function myFunc {

commands...
}

myFunc ()

How could I properly run only myFunc, in the background or alternatively, in another process? If it's even possible?

4

You can use a shell function pretty much anywhere you can use a program. Just remember that shell functions don't exist outside the scope in which they were created.

#!/bin/bash
#
f() {
    sleep 1
    echo "f: Hello from f() with args($*)" >&2
    sleep 1
    echo "f: Goodbye from f()" >&2
}

echo "Running f() in the foreground" >&2
f one

echo "Running f() in the background" >&2
f two &

echo "Just waiting" >&2
wait

echo "All done"
exit 0
2

You run them in the background as in any other shell command or script with a & at the end.

Bash and similar shells also allow you to combine commands with ( and ) such as:

(command1; command2) &

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.