Search code examples
linuxbashdocker-composeubuntu-18.04schedule

Linux At Command running docker-compose


Goal: I am trying to schedule a 'docker-compose up' at a scheduled time

Operating System: Linux Ubuntu 18.04

I have tried using the 'at' command in linux but I am trying to set variables and couldn't get it to quite work.

Attempts below:

sudo echo "KEY='VALUE' DIRECTION='D' sh run.sh" | at now + 2 minute
# Above does not run the docker-compose

sudo echo "KEY='VALUE' DIRECTION='D' sh run.sh" | at now + 2 minute
# Above runs docker-compose command immediately but not in 2 minutes
# run.sh

# SET LOGFILE LOCATION
LOG_FILE="/var/log/at/test.log"

FLIGHT_FILE="flight.yaml" docker-compose up --abort-on-container-exit >> "${LOG_FILE}"

If there is a better way than using the 'at' command I am also open to that.

Thank you in advance


Solution

  • in your example (both attempts are the same) only the echo is executed by sudo, at is executed as your user.

    options:

    run the script on the root's at queue:

    sudo sh -c "echo \"KEY='VALUE' DIRECTION='D' sh /home/user/run.sh \" | at now + 1 min"
    

    make at run sudo - works only if you have sudo setup not to ask a password:

    echo "sudo sh -c \"KEY='VALUE' DIRECTION='D' /home/user/run.sh\"" | at now + 2 min
    

    configure docker so you don't need sudo