I am running a script in the background on linux using:
python3 bot.py command &
and during github actions deployment I am using:
kill $(pgrep -fi bot.py)
To cancel the previous job before starting one again.
However, when I do that... it cancels the github actions job with the following error:
Process exited with status 137 from signal KILL
How can I get around this?
CodePudding user response:
So, kill <pid>
is a SIGKILL
signal & python
throws 137
exit code (128 9).
As you are doing this manually (not because of a system resource crunch), you can put a trap
to check the 137
exit code & decide your next actions.
#!/bin/bash
## catch the exit code & apply logic accordingly
function finish() {
# Your cleanup code here
rv=$?
echo "the error code received is $rv"
if [ $rv -eq 137 ];
then
echo "It's a manual kill, attempting another run or whatever"
elif [ $rv -eq 0 ];
then
echo "Exited smoothly"
else
echo "Non 0 & 137 exit codes"
exit $rv
fi
}
pgrep -fi bot.py
if [ $? -eq 0 ];
then
echo "Killing the previous process"
kill -9 $(pgrep -fi bot.py)
else
echo "No previous process exists."
fi
trap finish EXIT