I'm running the following bash script to restart my Perforce server whenever it crashes, but what I want to do is make it so the bash script runs a Python script to send a webhook with the exit code of the application to my Slack channel.
The Python script allows you to do something like python app.py 225 and it works, but what I want to do is pass $? into that script, such as python app.py $?.
Whenever the service restarts the echo line reports the exit code properly (In the terminal it shows 225), but when passed into the Python script $? appears as 0.
#!/bin/bash
until ~/Desktop/shell/p4d; do
echo "Perforce Server crashed with exit code $?. Respawning.." >&2
sleep 1
python ~/Desktop/shell/app.py $?
done
Snippet of the Python script which fetches the argument:
if __name__ == "__main__":
error = str(sys.argv[1])
print error
post_message(error)
How would I do this?
$?will hold0assleep 1, presumably, exited with exit code0... Bash is quirky that way ;)$?in the example is the exit code ofsleep 1rather thanp4d. Store it as a variable just inside the until loop ftw