1

I can always start my container successfully with:

fadedbee@host:~$ sudo lxc-start -n mycontainer

It used to start automatically, until I bound some directories on a local ZFS to it.

I added the following to /etc/rc.local:

/bin/date >> /var/log/mycontainer.log
/bin/chmod a+r /var/log/mycontainer.log

/usr/bin/lxc-start -o /var/log/mycontainer.log

-l INFO -n mycontainer > /var/log/mycontainer.stdout 2> /var/log/mycontainer.stderr &

The /var/log/mycontainer.log file is created, but only contains the date and nothing else.

I added the following to root's crontab:

@reboot /usr/bin/lxc-start -o /var/log/mycontainer.log -l INFO -n mycontainer

but that didn't produce any result either.

I strongly suspect that all of these things are happening before ZFS has finished. Adding a sleep 30 ; to the cron entry didn't help.

What confuses me is that I get no log stderr, stdout or log output at all.

How can I make my container start automatically, or at least see log output?


Update:

I've added:

*/5 * * * * /usr/bin/lxc-start -n mycontainer

to root's cron, but still it won't start. The same (sudo'd) command from the bash prompt works. The rest of root's cron entries are working as expected.

2
  • 1
    A query regarding the log files - there are references to /var/log/green.log & /var/log/green. These are two different files, so is this intentional?
    – Haxiel
    May 9, 2020 at 7:54
  • It wasn't intentional.
    – fadedbee
    May 16, 2020 at 6:21

3 Answers 3

2
+25

To start LXC containers at boot, the lxc system service runs the lxc-autostart, as documented here.

To configure autostart, put the following two lines in the container configuration:

lxc.start.auto = 1
lxc.start.delay = 30

The delay may be set to any desired value in seconds.

There must not be anything in the crontab. Just reboot the system, and the lxc service will do the job.

You may want to check if the LXC autostart is run on boot. To do so on a systemd system, you can list the systemd service and check for lxc.service like this:

systemctl -la | grep 'lxc\.service'

You should get a line like this:

lxc.service             loaded    active   exited    LXC Container Initialization and Autoboot Code

If you do not get such a line, try enabling the service:

sudo systemctl enable lxc
2
  • I already have these lines in place. It used to start without an issue. My main problem is that I see no errors telling me that is hasn't started and why.
    – fadedbee
    May 22, 2020 at 8:26
  • @fadedbee I expanded my answer to check for the service. May 25, 2020 at 7:13
0

I strongly encourage to use the “official” solution provided by @rexkogitans.

Programs not starting from cron often suffer from a different environment. The executing shell is usually sh and environment variables differ from your interactive shell. If you are interested, run commands like set and env from cron and direct their output to a file.

1
  • The problem I'm having is that the "official" solution does not work, nor do many others, and I cannot see any error messages. Starting the container, by hand, on the command-line works.
    – fadedbee
    May 22, 2020 at 8:24
0

EDIT: I have solved this problem by adding the necessary PATH data to crontab -e. I have added the line:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

after crontab comments but before the job list.

This problem (and solution) can be partially validated by comparing the outputs of command line:

env

and the (temporary) cron job:

* * * * * env > env_dump.txt

In my case, the paths were not the same and the cron paths were insufficient to launch lxc-start.

I have a similar problem. My container will start fine from the command line after elevating by su - and issuing lxc-start -n test (debian 11).

I have a backup script that stops the container, tar.gz the rootfs, then restarts it. I run the script from cron via sh /home/dave/backup.sh. It performs all steps as expected, but fails to restart the container when it is done. I cannot find useful error messages. If I run this exact command from the shell it works fine. Somehow, something about cron is causing an issue. Was this problem ever resolved?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .