9.5 Monitoring and Shutting Down Containers

To display the containers that are configured, use the lxc-ls command on the host.

[root@host ~]# lxc-ls
ol6ctr1
ol6ctr2 

To display the containers that are running on the host system, specify the --active option.

[root@host ~]# lxc-ls --active
ol6ctr1 

To display the state of a container, use the lxc-info command on the host.

[root@host ~]# lxc-info -n ol6ctr1
state:  RUNNING
pid:    10171 

A container can be in one of the following states: ABORTING, RUNNING, STARTING, STOPPED, or STOPPING. Although lxc-info might show your container to be in the RUNNING state, you cannot log in to it unless the /usr/sbin/sshd or /sbin/mingetty processes have started running in the container. You must allow time for the /sbin/init process in the container to first start networking and the various other services that you have configured.

To view the state of the processes in the container from the host, either run ps -ef --forest and look for the process tree below the lxc-start process or use the lxc-attach command to run the ps command in the container.

[root@host ~]# ps -ef --forest
UID   PID   PPID  C STIME TTY    TIME     CMD
...
root  3171     1  0 09:57 ?      00:00:00 lxc-start -n ol6ctr1 -d
root  3182  3171  0 09:57 ?      00:00:00  \_ /sbin/init
root  3441  3182  0 09:57 ?      00:00:00      \_ /sbin/dhclient -H ol6ctr1 ...
root  3464  3182  0 09:57 ?      00:00:00      \_ /sbin/rsyslogd ...
root  3493  3182  0 09:57 ?      00:00:00      \_ /usr/sbin/sshd
root  3500  3182  0 09:57 pts/5  00:00:00      \_ /sbin/mingetty ... /dev/console
root  3504  3182  0 09:57 pts/1  00:00:00      \_ /sbin/mingetty ... /dev/tty1
root  3506  3182  0 09:57 pts/2  00:00:00      \_ /sbin/mingetty ... /dev/tty2
root  3508  3182  0 09:57 pts/3  00:00:00      \_ /sbin/mingetty ... /dev/tty3
root  3510  3182  0 09:57 pts/4  00:00:00      \_ /sbin/mingetty ... /dev/tty4
...
[root@host ~]# lxc-attach -n ol6ctr1 -- /bin/ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  19284  1516 ?        Ss   04:57   0:00 /sbin/init
root       202  0.0  0.0   9172   588 ?        Ss   04:57   0:00 /sbin/dhclient
root       225  0.0  0.1 245096  1332 ?        Ssl  04:57   0:00 /sbin/rsyslogd
root       252  0.0  0.1  66660  1192 ?        Ss   04:57   0:00 /usr/sbin/sshd
root       259  0.0  0.0   4116   568 lxc/console Ss+ 04:57   0:00 /sbin/mingett
root       263  0.0  0.0   4116   572 lxc/tty1 Ss+  04:57   0:00 /sbin/mingetty
root       265  0.0  0.0   4116   568 lxc/tty2 Ss+  04:57   0:00 /sbin/mingetty
root       267  0.0  0.0   4116   572 lxc/tty3 Ss+  04:57   0:00 /sbin/mingetty
root       269  0.0  0.0   4116   568 lxc/tty4 Ss+  04:57   0:00 /sbin/mingetty
root       283  0.0  0.1 110240  1144 ?        R+   04:59   0:00 /bin/ps aux
Tip

If a container appears not to be starting correctly, examining its process tree from the host will often reveal where the problem might lie.

If you were logged into the container, the output from the ps -ef command would look similar to the following.

[root@ol6ctr1 ~]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 07:58 ?        00:00:00 /sbin/init
root       183     1  0 07:58 ?        00:00:00 /sbin/dhclient -H ol6ctr1 ...
root       206     1  0 07:58 ?        00:00:00 /sbin/rsyslogd -i ...
root       247     1  0 07:58 ?        00:00:00 /usr/sbin/sshd
root       254     1  0 07:58 lxc/console 00:00:00 /sbin/mingetty /dev/console
root       258     1  0 07:58 ?        00:00:00 login -- root
root       260     1  0 07:58 lxc/tty2 00:00:00 /sbin/mingetty /dev/tty2
root       262     1  0 07:58 lxc/tty3 00:00:00 /sbin/mingetty /dev/tty3
root       264     1  0 07:58 lxc/tty4 00:00:00 /sbin/mingetty /dev/tty4
root       268   258  0 08:04 lxc/tty1 00:00:00 -bash
root       279   268  0 08:04 lxc/tty1 00:00:00 ps -ef

Note that the process numbers differ from those of the same processes on the host, and that they all descend from the process 1, /sbin/init, in the container.

To suspend or resume the execution of a container, use the lxc-freeze and lxc-unfreeze commands on the host.

[root@host ~]# lxc-freeze -n ol6ctr1
[root@host ~]# lxc-unfreeze -n ol6ctr1

From the host, you can use the lxc-shutdown command to shut down the container in an orderly manner.

[root@host ~]# lxc-shutdown -n ol6ctr1

Alternatively, you can run a command such as halt or init 0 while logged in to the container.

[root@ol6ctr1 ~]# halt

Broadcast message from root@ol6ctr1
	(/dev/tty2) at 22:52 ...

The system is going down for halt NOW!
lxc-console: Input/output error - failed to read

[root@host ~]#

As shown in the example, you are returned to the shell prompt on the host.

To shut down a container by terminating its processes immediately, use the lxc-stop command on the host.

[root@host ~]# lxc-stop -n ol6ctr1

If you are debugging the operation of a container, using lxc-stop is the quickest method as you would usually destroy the container and create a new version after modifying the template script.

To monitor the state of a container, use the lxc-monitor command.

[root@host ~]# lxc-monitor -n ol6ctr1
'ol6ctr1' changed state to [STARTING]
'ol6ctr1' changed state to [RUNNING]
'ol6ctr1' changed state to [STOPPING]
'ol6ctr1' changed state to [STOPPED]

To wait for a container to change to a specified state, use the lxc-wait command.

lxc-wait -n $CTR -s ABORTING && lxc-wait -n $CTR -s STOPPED && \
  echo "Container $CTR terminated with an error."