1

I have got an python script, that is running in the background and that is working with watchdog and os. Is there a way to force quit such a script? I have already tried to press ctrl + x or ctrl + z as proposed in other discussions, what didn't work for me.

2
  • have you tried fg followed by ctrl+c? Commented Jan 11, 2020 at 17:25
  • Does this answer your question? How to kill a process in MacOS? Commented Jan 11, 2020 at 17:29

2 Answers 2

1

If you have sent your process to background with an & at the end of the command line, you can bring it back to the foreground with fg. Once it's back to the foreground, you can kill it with Ctrl+C

test.py:

from time import sleep

n = 0
while True:
    sleep(1)
    n += 1
    print(n)

Example output:

$ python3 -q test.py &
[1] 82675
$ 1
2
3
4
fg
5
6
^CTraceback (most recent call last):
  File "test.py", line 6, in <module>
    sleep(1)
KeyboardInterrupt

This works from the same shell where you started your script. From another shell, you can find the process ID (PID) with ps, and then kill it with kill -9 <PID>. BTW, the first output line in the above example tells you the PID (82675 in this case).

Assuming you have no other process containing the name of your script in the command line, you could even do this (replacing test.py):

$ kill -9 $(ps | fgrep test.py | fgrep -v fgrep | cut -d' ' -f1)
Sign up to request clarification or add additional context in comments.

Comments

0

If you will run the command jobs you will see something like this

[1]  + suspended  python .....

You then can run kill %<job-number> And you will see:

[1]  + 43347 killed     vim python ...

2 Comments

Ok, but when i run the command jobs by typing it into the terminal, I actually get no output: IMAC221:~ wewar$ jobs IMAC221:~ wewa$ Did I do anything wrong?
this usually mean that you have no background jobs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.