Running Commands at Container Startup

uviteru

Member
May 21, 2019
24
0
6
39
Hi,

I am trying to run a command within a container whenever the container starts, but I am not having success trying several different methods.

Two of the methods are using rc.local and crontab, but neither of these work.

I am simply trying to run
Code:
expressvpn connect
.

To get this to work with cron, I tried inputting
Code:
@reboot expressvpn connect
and also tried putting that line in a script and called the script from within crontab.

For rc.local, I created the file and made it executable, as well as placed #!/bin/sh at the top.

Does anyone know why I cannot get either of these to work?
 
hi!

check this:

Code:
lxc.hook.autodev
              A  hook to be run in the container's namespace after mounting has been done and after any mount
              hooks have run, but before the pivot_root, if lxc.autodev == 1.  The purpose of this hook is to
              assist in populating the /dev directory of the container when using the autodev option for sys‐
              temd based containers. The container's /dev directory is relative  to  the  ${LXC_ROOTFS_MOUNT}
              environment variable available when the hook is run.

you can run your commands in the container namespace after it starts and mounts everything. autodev is enabled by default, so it should work.

just add something like this in the /etc/pve/lxc/CTID.conf :
Code:
lxc.hook.autodev = sh -c "yourcommand;yournextcommand;..."
 
Thank you for your quick response! I have tried putting this in, but then the container will not start:

Code:
lxc.hook.autodev  = sh -c "expressvpn connect"

But I get this error:
upload_2019-5-28_11-16-47.png
 
sorry, i wrote it wrongly. and it looks like it wasn't the best option.

i'll get back to you


EDIT:

i saw you wrote that it worked in the other post. is this resolved as well?
 
Last edited:
N
sorry, i wrote it wrongly. and it looks like it wasn't the best option.

i'll get back to you


EDIT:

i saw you wrote that it worked in the other post. is this resolved as well?

No, not for running the expressvpn command. It worked when setting up the tun device (the other post), but not in this case or trying to run expressvpn. I'm not sure why since it is just trying to execute a command. Any other ideas?
 
it should have worked with cron i think... which user's crontab did you put it to? does that user have execution rights for that binary? maybe you need to pass the full path to the binary.
 
it should have worked with cron i think... which user's crontab did you put it to? does that user have execution rights for that binary? maybe you need to pass the full path to the binary.

I run everything as root in this particular container. I did
Code:
crontab -e

And then
Code:
@reboot expressvpn connect
 
I run everything as root in this particular container. I did
Code:
crontab -e

And then
Code:
@reboot expressvpn connect
Depending on the situation, I've always liked creating systemd daemons or /etc/init.d/ scripts. For machines that just need to run a script on startup I create a script in /etc/init.d/ and symlink it in /etc/rc2.d to get it to run on startup. For machines that I would rather have a daemon, I create a .service file in /lib/systemd/system/. This is nice because then you have start/stop control with systemctl start/stop name.service
 
Depending on the situation, I've always liked creating systemd daemons or /etc/init.d/ scripts. For machines that just need to run a script on startup I create a script in /etc/init.d/ and symlink it in /etc/rc2.d to get it to run on startup. For machines that I would rather have a daemon, I create a .service file in /lib/systemd/system/. This is nice because then you have start/stop control with systemctl start/stop name.service

I tried what you recommended with creating a script in init.d and then symlinking it to etc/rc2.d, but that did not work. I made it executable and verified I could run in from the command line, but it does not execute on startup. All I'm trying to do is run the command: expressvpn connect.

Why am I having so much trouble with it?
 
just make a service file for systemd
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!