0

I have a python script running in background via nohup.
It is an alarm system. Once I run the script it checks if a motion is detected. Now i have the problem of how to disable the alarm.
Could you help me to find a good way to send a command to disable/kill the running script?

Quick and dirty method was:
Checking a txt file content: if I want to shut down the script, I modify the txt.

Something like:

if txt content is "disable":
quit 

I am pretty new to python maybe you can help me.

2
  • What OS are you running the script on? Commented Jul 12, 2014 at 20:58
  • I run it on LINUX. Debian Wheezy. Commented Jul 12, 2014 at 21:00

2 Answers 2

1

If you're using linux/unix/macos, you can kill the script from the command line

$ ps aux | grep python
nate     16210  0.2  0.0  20156  5176 pts/2    S+   17:02   0:00 python3
nate     16215  0.0  0.0   8044   932 pts/1    S+   17:02   0:00 grep python

The second field is the PID of the running process, kill it with the kill command:

$ kill 16210
$ ps aux | grep python
nate     16220  0.0  0.0   8044   932 pts/1    S+   17:02   0:00 grep python

To grab the pid automatically, you can add this to the top of your python script:

import os
pid = os.getpid()
with open('pid', 'w') as procfile:
    procfile.write(pid)

Now, you can grab that pid with one line of bash:

kill -TERM $(cat pid)

replace pid with the location of the file pid

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, But how could i use this automatically? I have to look for the process ID every time. That takes too long.
0
if os.path.exists(os.path.expanduser("~/disable_alarm.txt")):
     #then dont sound alarm

and just put a file in your home directoy when you want it disabled ...

Comments

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.