9

If I send a SIGTERM signal to a process using the kill command, I expect an exit code, but I always get 0 (zero) when running the below command after killing a process:

echo $?

According to the answer in this post, I should get 143 when sending a SIGTERM to a process: Always app Java end with "Exit 143" Ubuntu

But I don´t get that exit code. Why?

Community
  • 1
  • 1
Rox
  • 2,647
  • 15
  • 50
  • 85

1 Answers1

14

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    Ah, ok! Is there any way to check the exit code of the process that is beeing killed? – Rox Nov 24 '11 at 12:17
  • @Rox: I believe only the parent process has direct access to the exit status, so not directly. – Fred Foo Nov 24 '11 at 12:20
  • 2
    It is possible to check the exit code of the process being killed. Use ptrace to attach to it before you send the signal. (This is probably more trouble than it is worth!) Alternatively, have the parent of the killed process report its exit status. – William Pursell Nov 24 '11 at 17:25
  • Any reference for the exit code of the `kill` command itself? – Ohad Schneider Jan 26 '17 at 18:29
  • @OhadSchneider: `$ man kill`, which tells us: 0 = success, 1 = failure, 64 partial success (when more than one process specified). – NicolasWebDev Apr 18 '17 at 15:22
  • 2
    @Sathors thanks. Most man pages don't mention it, but it seems at least some do (http://man7.org/linux/man-pages/man1/kill.1.html). Lord knows if the latter applies to all the systems that don't have it mentioned in their man. One day I'll understand why the same command has 19 different man pages (I'm guessing they have to do with different distros but still)... – Ohad Schneider May 01 '17 at 09:27