0

I am currently making an audio recorder, and the script runs forever. I am currently trying to figure out a way to stop my audio recorder script, but I've had no such luck.

So far I've tried this user post: Stackoverflow Question, and tried this:

import os

os.system('pkill -f audio-recorder.py')

But, instead of just stopping it, it terminates it, which doesn't let my audio-recorder script shut down properly. See, my script is set up so that when the finally: block is reached it cleans everything up and stops recording, but if I use that user's answer it doesn't do that. Is there a way to accomplish what I'm doing, stopping a python script gently, not a full on kill?

8
  • process is named python not audio-recorder.py since this is a script run by interpreter. Commented Sep 13, 2024 at 12:02
  • So you want me to change the line os.system('pkill -f audio-recorder.py') to os.system('pkill -f python audio-recorder.py')? Commented Sep 13, 2024 at 12:04
  • 1
    Try sending SIGINT instead of SIGKILL/SIGTERM. I don't know your OS to know if and how that matters. Commented Sep 13, 2024 at 12:05
  • The documentation for pkill says SIGTERM is the "graceful" option for stopping a process. So maybe try adding the -SIGTERM option flag. Commented Sep 13, 2024 at 12:07
  • 1
    Since this python script only runs the pkill command, I don't understand why it needs to be a python script at all. Just run pkill directly. Commented Sep 13, 2024 at 12:25

3 Answers 3

1

try

import os

os.system('pkill -SIGINT -f audio-recorder.py')
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the signal module to set up a signal handler. The default signal that will be sent to the specified process by either kill or pkill will be SIGTERM (15).

So let's say that audio-recorder.py look like this:

import signal
import time
import sys

def handle(*_): # we're not interested in the signal number or the current stack frame
    print("SIGTERM handled...exiting")
    # write your cleanup code here
    sys.exit()

if __name__ == "__main__":
    signal.signal(signal.SIGTERM, handle) # specify the signal handler
    while True: # run forever
        time.sleep(1)

When this is running you'll be able to stop it cleanly with:

pkill -f audio-recorder.py

Note:

This will be fine on Unix-type systems (Linux, MacOS etc.) but may not work on Windows. Windows is very un-Unix like when it comes to signal handling although (I believe) signals such as SIGTERM are portable

1 Comment

Please explain why this has been downvoted. I'm still learning
0

You can use the subprocess module to launch your audio recorder script as a subprocess, then terminate it.

import subprocess
import time

# Start the audio recorder script as a subprocess
process = subprocess.Popen(["python", "audio_recorder.py"])

# Let the recorder run for 5 seconds
time.sleep(5)

# Terminate the audio recorder process
process.terminate()
print("Audio recorder stopped.")

1 Comment

I’ve tried this and it opens my script but doesn’t close it. Thanks for the helps though.

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.