26

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.

user9517
  • 117,122
sega01
  • 361

6 Answers6

34

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>
LeStarke
  • 341
8

No. When the shell catches SIGCHLD it unconditionally sets the return value appropriately to a non-zero value, so this would require modification of either the script or of the shell.

4

I m not sure but this will exit with a 0 if any of the listed signals are received. You could perform other actions including calling a function, etc.

#!/bin/bash 
trap 'exit 0' SIGINT SIGQUIT SIGTERM
TBI Infotech
  • 1,594
4

This one-liner worked for me:

/bin/bash -c '/usr/bin/killall -q process_name; exit 0'
HBruijn
  • 84,206
  • 24
  • 145
  • 224
3

A bit hacky way, but..

You can create a wrapper to your process, overriding SIGCHLD Handler. For example:

#!/bin/bash
set -o monitor
trap 'exit(0)' CHLD
/some/dir/yourcommand

After that you can make your script running this wrapper instead of your process by putting it earlier in $PATH and renaming to the same name.

DukeLion
  • 3,349
2

To make sure the kill or killall command doesn't break the shell (like in a bash script with set -e), or make sure the line with kill exits with 0. We may try

killall <PROCESS_NAME> || /bin/true

or

kill <PROCESS_NAME> || /bin/true
socrates
  • 121