Hi,
I am making a program where I am using customtkinter as the frontend and running an agent backend to speak and interact with the user.
When I button is pressed on the frontend interface it opens and runs the agents really well how I need it to work however when another button is pressed I need to to shut down the agent in the command line and stop working so the session closes. However I cannot find out how to do this from within code. The only way I know how to do this is to manually kill the terminal from VS code.
I am running the agent from a different script by calling it using the line subprocess.Popen… so effectively I need to shut down the script from another script
Would appreciate some help with the correct code syntax to how to do this please. Thanks so much!
@Archie_Enstone, This is really a subprocess-control question, with one LiveKit detail that helps: the agent worker installs SIGINT and SIGTERM handlers and shuts down gracefully on them, draining the active session before it exits (cli.py#L238). So you do not need to kill the terminal. From your launcher script, send SIGTERM with proc.terminate() to trigger that graceful drain, and fall back to proc.kill() only if it does not exit in time.
import subprocess
proc = subprocess.Popen(["python", "agent.py", "start"], start_new_session=True)
# when the stop button is pressed:
proc.terminate() # SIGTERM on POSIX -> worker drains and closes the session
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
proc.kill() # SIGKILL fallback if it did not exit
The worker force-exits if it receives a second signal (cli.py#L337), so a repeated stop press will hard-stop it. Since the worker forks a process per job, start_new_session=True puts it in its own process group, so if you ever see leftover job processes you can signal the whole group instead:
import os, signal
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
Your screnshot shows you invoking uv run agent.py console which will launch the agent in the console, but this is only intended as a debugging feature - I wouldn’t recommend building a production solution around it.
When an interview starts, you should be dispatching an agent to the room, Agent dispatch | LiveKit Documentation , and joining the room yourself as a participant. Having done that, you could then probably also delete the room using the LK CLI which would shut down the agent.
If this is a hobbyist project, then the above is probably overkill
I’m not familiar with customtkinter.