Q:

How can I detach a process from a bash script?

I am trying to detach a process from a bash script so that SIGINT will not be forwarded to the process when I exit the script.
I have used the disown command in terminal directly, however in bash, disown does not stop SIGINT from being forwarded. The purpose of this script is to start openocd and then gdb with a single invocation. Since the script never exits (it's running gdb) SIGINT is still forwarded from gdb to openocd which is a problem since SIGINT is used as the halt command in gdb.
In terminal it would look something like this:
$ openocd &    # run openocd demonized
$ disown $!    # disown last pid
$ gdb          # invoke GDB

when invoked on terminal in this order, the SIGINT is not passed from gdb to openocd. However if this same invocation was in a bash script, the SIGINT is passed.
Any help would be greatly appreciated.
ps this problem is in OS X but I am trying to use tools which are also portable to all Unix tools.

A:

To detach a process from a bash script:
nohup ./process &

If you stop your bash script with SIGINT (ctrl+c), or the shell exits sending SIGHUP for instance, the process won't be bothered and will continue executing normally. stdout & stderr will be redirected to a log file: nohup.out.
If you wish to execute a detached command while being able to see the output in the terminal, then use tail:
TEMP_LOG_FILE=tmp.log
> "$TEMP_LOG_FILE"
nohup ./process &> "$TEMP_LOG_FILE" & tail -f "$TEMP_LOG_FILE" &

A:

For me this works perfectly fine with disown
command & disown

A:

The solution I found involves a program called 'detach' written by Annon Inglorion and downloadable from his website
Once compiled, it can be used in a script as follows:
$ ./detach -p debug.pid openocd <args> # detach openocd
$ gdb <args>                           # run gdb
$ kill -9 $(cat debug.pid)             # end openocd process
$ rm debug.pid                         # remove file containing process id

This first line creates a new process (running openocd) and stores the process id in file (debug.pid) for use later. This prevents the issues with grepping for the pid as provided in Oliver's answer. Upon exiting the next blocking program (gdb) the file storing the pid is used to kill the detached process directly.

