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 use the lxc-ps command or run ps -ef --forest and look for the process tree below the lxc-start process.

[root@host ~]# lxc-ps -n ol6ctr1
CONTAINER   PID TTY          TIME CMD
ol6ctr1    7624 ?        00:00:00 init
ol6ctr1    7838 ?        00:00:00 dhclient
ol6ctr1    7861 ?        00:00:00 rsyslogd
ol6ctr1    7887 ?        00:00:00 sshd
ol6ctr1    7894 pts/7    00:00:00 mingetty
ol6ctr1    7898 ?        00:00:00 login
ol6ctr1    7900 pts/4    00:00:00 mingetty
ol6ctr1    7902 pts/5    00:00:00 mingetty
ol6ctr1    7904 pts/6    00:00:00 mingetty
ol6ctr1    7910 pts/3    00:00:00 bash

[root@host ~]# ps -ef --forest
UID        PID  PPID  C STIME TTY          TIME CMD
...
root      7612     1  0 13:08 ?        00:00:00 lxc-start -n ol6ctr1 -d
root      7624  7612  0 13:08 ?        00:00:00  \_ /sbin/init
root      7838  7624  0 13:08 ?        00:00:00      \_ /sbin/dhclient -H ol6ctr1 ...
root      7861  7624  0 13:08 ?        00:00:00      \_ /sbin/rsyslogd -i ...
root      7887  7624  0 13:08 ?        00:00:00      \_ /usr/sbin/sshd
root      7894  7624  0 13:08 pts/7    00:00:00      \_ /sbin/mingetty /dev/console
root      7898  7624  0 13:08 ?        00:00:00      \_ login -- root
root      7910  7898  0 13:08 pts/3    00:00:00      |   \_ -bash
root      7900  7624  0 13:08 pts/4    00:00:00      \_ /sbin/mingetty /dev/tty2
root      7902  7624  0 13:08 pts/5    00:00:00      \_ /sbin/mingetty /dev/tty3
root      7904  7624  0 13:08 pts/6    00:00:00      \_ /sbin/mingetty /dev/tty4
...

In this example, the /usr/sbin/sshd and /sbin/mingetty processes have started, and root has logged in on /dev/tty1.

Tip

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

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

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."