how to kill a worker
# ask-ai
c
how to kill a worker
t
Thanks for reaching out, Filippo. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you with stopping or "killing" a LiveKit worker, and you have a few friendly options depending on what you need: • Graceful Shutdown: You can use the
close()
method to gently shut down the worker, making sure all tasks and resources are properly wrapped up. Here's how you can do it:
Copy code
await worker.close()
• Draining Workers: If you'd like to stop new jobs but let the current ones finish, the
drain()
method is perfect. You can even set a timeout like this:
Copy code
await worker.drain(timeout=1800)  # 30 minutes
• Force Termination: In case you need to stop things immediately, you can directly terminate the process like so:
Copy code
worker.process.terminate()
If you want to dive deeper or have any specific scenarios in mind, just let me know—I’m happy to guide you through it! Sources: pkg/service/session_manager.go | Job lifecycle | LiveKit Docs | livekit-agents/livekit/agents/worker.py | agents/src/worker.ts | Worker options | LiveKit Docs
c
and if you have a worker somewhere in the cloud and you want to kill by using lk cli ?
t
Hi there! I wanted to share that the LiveKit CLI doesn’t have a direct command to forcefully "kill" a worker in the cloud. But no worries—you can use the
lk agent restart
command to gracefully stop and restart a worker, which works really well:
Copy code
lk agent restart --id <agent-id>
Here’s what this command does: • Stops the worker from accepting any new jobs. • Lets any active jobs finish up smoothly (this is called draining). • Then shuts down the worker and starts a fresh instance. If you have any questions or if there’s a particular situation you’re dealing with, please don’t hesitate to reach out—I’m here to help! Sources: cmd/lk/agent.go | Agent deployment CLI reference | LiveKit Docs | Worker lifecycle | LiveKit Docs | Log collection | LiveKit Docs | Worker options | LiveKit Docs
c
and if it is in cloud in aws?
t
To gently stop a LiveKit worker running in AWS, here’s a simple guide you can follow: • Send a Termination Signal: If your worker is running on an EC2 instance, ECS task, or Docker container, you can send a SIGTERM signal to allow it to shut down gracefully:
Copy code
kill <worker-pid>
• For Docker containers, you can use:
Copy code
docker stop <container-id>
• Graceful Shutdown: This will let the worker finish any active jobs and then shut down within the set timeout (usually 60 seconds). • Force Kill: If the worker doesn’t respond, you might need to forcefully stop it with SIGKILL:
Copy code
kill -9 <worker-pid>
• Or for Docker:
Copy code
docker kill <container-id>
• For AWS ECS: You can scale your service down to zero or update the task definition like this:
Copy code
aws ecs update-service --cluster <cluster-name> --service <service-name> --desired-count 0
If you have any questions or need a hand with any of these steps, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/worker.py | Job lifecycle | LiveKit Docs | aws-ecs/README.md