2

I run some processes via SSH from a remote client.

Sometimes the processes can be killed, with eg. kill. How to get the exit code/signal? The program is a PHP script

I run the script via sh -c so I can get the pid and ppid

sh -c 'echo $PPID; echo $$; php script.php'

Is it possible to listen/get the code from the parent process (sh -c)?

I can check if the pid is running, but if it has stopped I don't know if it completed with success, with an error or it was killed

I know you can use $? to get the exit code but would that work if the process was killed?

0

1 Answer 1

2
sh -c 'echo $PPID; echo $$; php script.php; echo $? > phpexit'; echo $? > shexit

The exit code of php will be contained in the phpexit file.

The exit code of sh will be contained in the shexit file.

If the sh process is killed, the phpexit file will not be generated.

You can verify the correctness of each of those by letting the script terminate with success, doing a pkill php or killing the PID printed by echo $$.

See also exit codes with special meanings.

5
  • What is the difference between signal 9 and 15?
    – clarkk
    Nov 28, 2020 at 23:09
  • @clarkk 9 is KILL (can't be ignored), 15 is TERM (leaves a chance for cleanup by the process). See man 7 signal and unix.stackexchange.com/a/8918.
    – Quasímodo
    Nov 28, 2020 at 23:11
  • SIGKILL cannot be ignored, caught or blocked. The process will simply die, without being able to do anything about it. Nov 29, 2020 at 1:41
  • When I use kill I get code 143 ?
    – clarkk
    Nov 29, 2020 at 14:08
  • The same goes with kill -15 gives 143 and if I use kill -9 I get a bash error.. I don't think I can do anything about that?
    – clarkk
    Nov 29, 2020 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.