Linux – Kill a process and force it to return 0 in Linux

killlinuxprocess

In the Linux environment, how can I send a kill signal to a process, while making sure that the exit code returned from that process is 0? Would I have to do some fancy GDB magic for this, or is there a fancy kill signal I'm unaware of?

Test case:

cat; echo $?

killall cat

Trying various kill signals only offers different return signals, such as 129, 137, and 143. My goal is to kill a process which a script runs, but make the script think it was successful.

Best Answer

You can attach to the process using GDB and the process ID and then issue the call command with exit(0) as an argument.

call allows you to call functions within the running program. Calling exit(0) exits with a return code of 0.

gdb -p <process name>
....
.... Gdb output clipped
(gdb) call exit(0)

Program exited normally.

As a one-line tool:

gdb --batch --eval-command 'call exit(0)' --pid <process id>