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?

clarkk
  • 1,837

1 Answers1

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.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73