Run a script on all container from host

esallenave

New Member
May 19, 2016
2
0
1
50
Paris - France
Hi,
I am looking for the best way to run a given mysuperscript.sh, located on my pve-host, on all containers running.
Of course I assume that all containers are running same OS, in my case Debian Jessie.
Not sure how to do it cleanly and ensure results are properly checked.
Would someone have some inputs ?
Many thanks,
Eric
 
Hi,

You can use pct list to get the running container and then use pct exec <vmid> to execute the command in the container.
 
there is also "pct push" to push the script into the containers first..
 
No need to copy anything... ;-)
Assuming the script is marked as executable (chmod +x)
Code:
# exec 3<mysuperscript.bash
# for i in $(pct list | awk '/\d/{print $1}'); do pct exec "$i" ./proc/self/fd/3 ; done

Or if you know what shell/interpreter to run it with (eg. bash) you can use
Code:
# for <stuff>; do pct exec "$i" bash /proc/self/fd/3; done
or pipe it to the shell to be read from stdin with:
Code:
# for <stuff>; do cat mysuperscript.sh | pct exec "$i" bash; done
Just remember that the shell you're using has to exist in all containers under the same path (so you best use `#!/usr/bin/env bash` or something as shebang line...)
 
When i run my version of the first script
# for i in $(pct list | awk '/\d/{print $1}'); do pct push "$i" myscript.sh myscript.sh ; done

It pushes my script to all containers but the container with the id 100. i tried doing a pct push in the command line and it seemed to work in that case but not when i run that script.

Any suggestions?
 
Last edited:
It pushes my script to all containers but the container with the id 100. i tried doing a pct push in the command line and it seemed to work in that case but not when i run that script.

Code:
pct list | awk '/\d/{print $1}'

doesn't work properly because it ends up looking for the literal 'd' which only returns the lines with a "d" in it, so not all the containers end up in the for loop.

The proper regex is as follows:

Code:
pct list | awk '/[0-9]/{print $1}'

should get all the CT IDs properly.

EDIT:
Another alternative is:

Code:
pvesh ls /nodes/localhost/lxc | awk '{print $2}'
 
Last edited:
Hi,

I think that a better ideea is to use ansible, for all of your vHOSTS. In a such case is not important if all the hosts(VM or CT) have the same OS or not! And you can do also a lot of interesting stuffs(if the load on VM/CT > x, then do not run this super-script now, if the load on the PMX node have load/whatever metric > z, then do not make more damage )
 
Hate to bother but i am once again stuck

When i run the regex with vzdump it tells me that the vmid is an invalid format

Code:
for i in $(pct list | awk '/[0-9]/{print $1}' ); do vzdump "$i" --mode suspend --maito $email --compress gzip --storage $backupdrive ; done

vzdump must not like the format that pct can accept through regex

thank you,
 
Hate to bother but i am once again stuck

When i run the regex with vzdump it tells me that the vmid is an invalid format

Code:
for i in $(pct list | awk '/[0-9]/{print $1}' ); do vzdump "$i" --mode suspend --maito $email --compress gzip --storage $backupdrive ; done

vzdump must not like the format that pct can accept through regex

thank you,

The command you sent works fine for me. What is the output you get when you run just the pct list pipe regex? That's what gets into the for loop. If there's some weirdness there, it would be the cause of VMID not being valid.
 

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!