Linux: lxc forcefully stop and kill container

last updated in Categories , ,

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        


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.