Run a script on all container from host

Discussion in 'Proxmox VE: Installation and configuration' started by esallenave, May 19, 2016.

  1. esallenave

    esallenave New Member
    Proxmox Subscriber

    Joined:
    May 19, 2016
    Messages:
    2
    Likes Received:
    0
    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
     
  2. wolfgang

    wolfgang Proxmox Staff Member
    Staff Member

    Joined:
    Oct 1, 2014
    Messages:
    4,710
    Likes Received:
    313
    Hi,

    You can use pct list to get the running container and then use pct exec <vmid> to execute the command in the container.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. fabian

    fabian Proxmox Staff Member
    Staff Member

    Joined:
    Jan 7, 2016
    Messages:
    3,207
    Likes Received:
    498
    there is also "pct push" to push the script into the containers first..
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. wbumiller

    wbumiller Proxmox Staff Member
    Staff Member

    Joined:
    Jun 23, 2015
    Messages:
    642
    Likes Received:
    80
    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...)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Stijn Cos

    Stijn Cos New Member
    Proxmox Subscriber

    Joined:
    Jan 17, 2019
    Messages:
    2
    Likes Received:
    0
    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?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    #5 Stijn Cos, Jan 17, 2019
    Last edited: Jan 17, 2019
  6. oguz

    oguz Proxmox Staff Member
    Staff Member

    Joined:
    Nov 19, 2018
    Messages:
    510
    Likes Received:
    50
    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}'
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    #6 oguz, Jan 18, 2019
    Last edited: Jan 18, 2019
  7. guletz

    guletz Active Member

    Joined:
    Apr 19, 2017
    Messages:
    867
    Likes Received:
    115
    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 )
     
  8. morph027

    morph027 Active Member

    Joined:
    Mar 22, 2013
    Messages:
    409
    Likes Received:
    50
  9. Stijn Cos

    Stijn Cos New Member
    Proxmox Subscriber

    Joined:
    Jan 17, 2019
    Messages:
    2
    Likes Received:
    0
    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,
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. oguz

    oguz Proxmox Staff Member
    Staff Member

    Joined:
    Nov 19, 2018
    Messages:
    510
    Likes Received:
    50
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice