≡ Menu

Linux: lxc forcefully stop and kill container

I want to stop the application running inside a container. I issued lxc-stop -n myapp but the command is not returning to the shell prompt, and my lxc container entered in hang state. How do I kill my container running on Ubuntu Linux host?

You need use the lxc-stop command to reboot, cleanly shuts down, or kills all the processes inside the Linux container (LXC). By default, it will request a clean shutdown of the container by sending lxc.haltsignal (defaults to SIGPWR) to the container’s init process, waiting up to 60 seconds for the container to exit, and then returning. If the container fails to cleanly exit in 60 seconds, it will be sent the lxc.stopsignal (defaults to SIGKILL) to force it to shut down.

Reboot the container

The syntax is:
lxc-stop -n containerNameHere -r
To reboot the container called myhttpd, enter:
lxc-stop -n myhttpd -r

Shut down and kill the container (hard kill)

The syntax is:
lxc-stop -n containerNameHere -k
lxc-stop -n containerNameHere --kill
To forcefully kill and shut down the container called myhttpd, enter:
lxc-stop -n myhttpd -k
To wait TIMEOUT (say 180) seconds before hard-stopping the container, enter:
lxc-stop -n myhttpd -k -t 180
lxc-stop -n myhttpd -k --timeout 180

Clean shutdown (recommended)

The following syntax only request a clean shutdown, do not kill the container tasks if the clean shutdown fail:
lxc-stop -n containerNameHere --nokill
lxc-stop -n myhttpd --nokill

A note about timeouts

To avoid waiting up to 60 seconds and simply perform the requestion action (reboot, shutdown, or hard kill) and exit, pass the -W option:
## hard kill ##
lxc-stop -W -n containerNameHere -k
## reboot ##
lxc-stop -W -n containerNameHere -r

How do I view the container state?

To list only running containers, enter:
lxc-ls --fancy --running
Sample outputs:
To list only stopped containers, enter:
lxc-ls --fancy --stopped
To list both stopped and running containers on the system, enter:
lxc-ls --fancy
Sample outputs:

NAME         STATE    IPV4       IPV6  AUTOSTART  
debian8      STOPPED  -          -     NO         
wwwapp1      RUNNING  -     YES        
dnsapp1      RUNNING  -     YES        
staticf1     RUNNING  -     YES        
mysqlapp     RUNNING  -     YES        
Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 0 comments… add one }

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , ,