Search code examples
bashshellforkexec

How to make exec command in shell script run in different process?


I'm working with this GUI application, GTKWave, that has a startup script, gtkwave, which I've added to my system path. At the very end of said script, there is this line:

$EXEC "$bundle_contents/MacOS/$name-bin" $* $EXTRA_ARGS

where $EXEC=exec

My problem is that given how exec works, my terminal is "hijacked" by the GUI process, meaning I can't run any more commands until I close the window, which is not ideal.

I currently got around this by putting this function in my .bash_profile:

function run-gtkwave ( ) { nohup gtkwave $1 &>/dev/null & }

While this works, it makes it hard to pass arguments to gtkwave. Ideally, I could do a form right before that exec command, but I'm not sure how to do that in bash. I know that the & character should do that, but

$EXEC "$bundle_contents/MacOS/$name-bin" $* $EXTRA_ARGS &

doesn't cut it, and neither does

"$bundle_contents/MacOS/$name-bin" $* $EXTRA_ARGS &

or

("$bundle_contents/MacOS/$name-bin" $* $EXTRA_ARGS) &

How do I modify this line to run the application in its own process?


Solution

  • You can make your run-gtkwave function pass arguments to gtkwave by changing the definition to:

    function run-gtkwave { nohup gtkwave "$@" &>/dev/null & }
    

    Since using nohup and output redirection stops the terminal being "hijacked", you might be able to fix the problem in gtkwave by doing the nohup redirection and the output redirection there. (If standard input is a terminal, nohup redirects it from /dev/null.) One possibility is:

    "$bundle_contents/MacOS/$name-bin" $* $EXTRA_ARGS </dev/null &>/dev/null &
    

    (I've omitted the $EXEC because I can't see any point in using exec and & together.)