Return code when OS kills your process
A process' return status (as returned by wait
, waitpid
and system
) contains more or less the following:
- Exit code, only applies if process terminated normally
- whether normal/abnormal termination occured
- Termination signal, only applies if process was terminated by a signal
The exit code is utterly meaningless if your process was killed by the OOM killer (which will apparently send you a SIGKILL signal)
for more information, see the man page for the wait command.
Return code when OOM killer kills a process
The Linux OOM killer works by sending SIGKILL
. If your process is killed by the OOM it's fishy that WIFEXITED
returns 1.
TLPI
To kill the selected process, the OOM killer delivers a SIGKILL
signal.
So you should be able to test this using:
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGKILL)
printf("Killed by SIGKILL\n");
}
Process Exit Code When Process is Killed Forcibly
In general, a process is terminated using TerminateProcess. The exit code is passed as a parameter to this method.
In the case of the task manager, the exit code is set to 1, but I don't know if it's documented anywhere.
Linux: get the exit code from the kill command
The exit code you get is for the kill
command itself. 0 means it succeeded, i.e. the other process got the signal you sent it. kill
just doesn't report the exit status for the process, since it can't even be sure the other process exited as a result of the signal it sent.
Retrieve a value from subprocess after kill()
While not an elegant answer, using process.send_signal(signal.SIGUSR1)
worked (as mentioned by @cg909). In the stop method we have:
def stop_keylogger():
process.send_signal(signal.SIGUSR1)
process.wait()
return process.stderr.read()
Then in the key logger to handle the signal we have:
def exit_and_return_counter(*args):
sys.stdout.write('%s'%counter)
exit()
if __name__ == '__main__':
signal.signal(signal.SIGUSR1, exit_and_return_counter)
try:
while 1:
main_loop()
except KeyboardInterrupt:
sys.exit()
Related Topics
Delete All Files Except the Newest 3 in Bash Script
How to Limit File Size on Commit
Using Curl in a Bash Script and Getting Curl: (3) Illegal Characters Found in Url
What Is the Linux Built-In Driver Load Order
Linux: Block Until a String Is Matched in a File ("Tail + Grep with Blocking")
Position of a String Within a String Using Linux Shell Script
How to Connect to the Fitbit Zip Over Bluetooth 4.0 Le on Linux with Bluez
Differencebetween "Var=${Var:-Word}" and "Var=${Var:=Word}"
How to Read the Mouse Button State from /Dev/Input/Mice
Creating Permanent Executable Aliases
Fallocate() Command Equivalent in Os X
How to Return Spawned Process Exit Code in Expect Script
What Is the Use of File Descriptor 255 in Bash Process
How to View and Edit the Ephemeral Port Range on Linux
Xampp: Another Web Server Daemon Is Already Running
Split Files Based on File Content and Pattern Matching
Linux Script That Monitors File Changes Within Folders (Like Autospec Does!)