0

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])
martineau
  • 119,623
  • 25
  • 170
  • 301
Json
  • 655
  • 10
  • 27

2 Answers2

1

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.
...
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • If it's the first case, how can it be resolved? If it's the second - how can I add sudo or get elevated privileges? – Json Sep 14 '18 at 05:35
  • @Json I would suggest two things: when the script is running, run `ps -ewwo pid,argsps -efw` and identify the process(es) you're looking for. You can also paste it here and we can have a look at it together. My initial guess is either a typo, or the script is not actually called as expected and there is something different about command line as seen in the process listing (matched by `pkill`). You can also try starting with `pgrep -f /home/pi/MotionDetector.py` and combined with information obtained above safely experiment to find a match (expected PIDs are displayed). – Ondrej K. Sep 14 '18 at 11:32
  • I tried the first command and got this error: error: ' unknown user-defined format specifier "argsps" ' – Json Sep 18 '18 at 10:31
  • I've tried to write the info to a file with the command below, but the file was empty pgrep -f /home/pi/MotionDetector.py >log.txt 2>&1 – Json Sep 18 '18 at 10:37
  • My apology that was an editing blunder of mine: `ps -ewwo pid,args` – Ondrej K. Sep 18 '18 at 10:50
  • So yeah, no match. Please try the other command and read through it's output. My gut would say the path is not exactly the same, perhaps the process of interest used a relative one on the command line? – Ondrej K. Sep 18 '18 at 10:52
  • Thanks, it works. so I get a long list of all my processes including the MotionDetector script with shows up like this: '23538 python /home/pi/MotionDetector.py' – Json Sep 18 '18 at 11:09
  • Now I am really stomped. So, here you indeed see `/home/pi/MotionDetector.py` as part of `python /home/pi/MotionDetector.py` running with pid `23538`, but if you at that time (or at any other time with corresponding pid) `pgrep -f /home/pi/MotionDetector.py`, it does not print `23538`. Assuming no copy/paste problem, there seems to be no mismatch between the actual and `pgrep`ed for string. Are we talking about one machine? Or is there some network access, VM, container involved between the runs? Could we be looking in a wrong place? – Ondrej K. Sep 18 '18 at 19:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180294/discussion-between-ondrej-k-and-json). – Ondrej K. Sep 18 '18 at 19:18
0

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.

Json
  • 655
  • 10
  • 27