简体   繁体   中英

Github actions canceling job on kill command

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM