Home > database >  cron job doesn't execute docker command
cron job doesn't execute docker command

Time:10-17

I'm facing weird behavior on setting up cronjob. I'm trying to execute simple docker run command at specific time every day, everything except docker is working, but docker command like just ignored or skipped.

I've tried to put command directly in crontab

6 4 * * * docker run -it --rm -e [email protected] -e PASS=SOMEPASS docker_image

Doesn't work. Then I tried to echo to some file to see if it actually triggered

6 4 * * * echo "works" > file.txt && docker run -it --rm -e [email protected] -e PASS=SOMEPASS docker_image

Echo is working, but docker still not executed. I also tried to save output from the docker to the file, but file is empty.

6 4 * * * docker run -it --rm -e [email protected] -e PASS=SOMEPASS docker_image > file.log

Then I moved the entire command to the bash script and run in manually to confirm command is correct and it works, so I've put this script file to cron, also added echo

40 4 * * * /root/script.sh >> /root/file.log (/root is a home folder)

Nothing is working, I tried to update bash file and put echo before and after docker command, and I see the result of those 2 (START,DONE), but docker still not executed and skipped.

#!/bin/bash
echo "START"
docker run -it --rm -e [email protected] -e PASS=SOMEPASS docker_image
echo "DONE"

I've tried with sudo in crontab, still no result. I've checked /var/log/syslog file and see that cron triggered

Oct 16 04:40:01 ubuntu-8gb-nbg1-1 CRON[433288]: (root) CMD (sudo /root/script.sh >> /root/file.log)

Here's output of permissions on the file

-rwxr-xr-x 1 root root 176 Oct 16 04:38 script.sh

Please help with understanding what I missed or how to troubleshoot the problem.

CodePudding user response:

Is your user in docker group? Can you run docker --ps on bash. Does cronjob belong to root?

CodePudding user response:

Try to remove -it flags from docker command since the Crontab doesn't support terminal (you may add -d flag to your docker command optionally if you want the container run and continue in the background).

6 4 * * * docker run --rm -e [email protected] -e PASS=SOMEPASS docker_image

or

6 4 * * * docker run -d --rm -e [email protected] -e PASS=SOMEPASS docker_image
  • Related