Json
Json

Reputation: 670

Kill a python script from another script - CalledProcessError

I want to kill a python script that runs on my system from another python script.

I followed this answer and tweaked the code a bit, but got an error:

Traceback (most recent call last):   File "/home/pi/base.py", line 13, in <module>
    check_call(["pkill", "-9", "-f", script])
    File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
        raise CalledProcessError(retcode, cmd)
  CalledProcessError: Command '['pkill', '-9', '-f', '/home/pi/MotionDetector.py']' returned non-zero exit status 1

Code:

from subprocess import check_call
import sys
import time

script = '/home/pi/MotionDetector.py'
check_call(["pkill", "-9", "-f", script])

Upvotes: 0

Views: 1023

Answers (2)

Json
Json

Reputation: 670

It turns out it was just a bug.

The solution was simple, I copied my script to a new file, deleted the old one and it worked, simple as that.

Upvotes: 0

Ondrej K.
Ondrej K.

Reputation: 9664

This means the pkill call has failed. Two possible reasons that come to mind:

  • it did not actually match any process. pkill would in this case not generate any output and return 1. You can verify this by trying to run pgrep instead of pkill and see what did it return on stdout (should be one or more lines with a PID in case of a match) and/or whether it also returned a non-zero status.

  • it did match, but user under which the pkill has been executed cannot kill the process(s) matched. In that case pkill should generate output on stderr similar to: pkill: killing pid 3244 failed: Operation not permitted

From the pkill(1) man page:

EXIT STATUS
...
       1      No processes matched or none of them could be signalled.
...

Upvotes: 1

Related Questions