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        

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum