Docker Community Forums

Share and learn in the Docker community.

How to pass crontab command when running docker

docker

(Hikmat Ullah) #1

Hi Experts,
I want to run docker by “docker run” command and want to pass crontab command like bleow.
crontab -l | ‘{ /bin/cat; /bin/echo "*/5 * * * * "; }’ | crontab -

The above command will create a cronscript which will run every 5 mins.

I have already done it in Dockerfile, but I need to use it with “docker run” command because I am creating docker from code base on creating schedules.
In short this docker will be created when job is scheduled.

-Hikmat


(Sam) #2

you want the container to issue docker run commands? is that right?

if you mount the docker host socket, then you can use it inside the container to issue docker commands back thru docker daemon on the host to manage containers.

--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock

(Hikmat Ullah) #3

No, I want to supply crontab command when I create docker container by “docker run” command

I you help in command line command, I will handle it in code.

thanks for your comment


(Sam) #4

ok, when I try to execute this I get an error…

sam@buildserver:~/Downloads/test$ crontab -l | ‘{ /bin/cat; /bin/echo "*/5 * * * * "; }’ | crontab -
no crontab for sam
‘{: command not found
*/5 * * * * 
}’: command not found

(Sam) #5

ok, i see it now…

you could write a script to use as the entrypoint command in the dockerfile

the script would look like this

#!/bin/bash
cron_file=/tmp/cron_file
# check and start cron if not running
if [ `ps -ef | grep cron| wc -l` -eq 0 ]; then  
cron & 
fi

crontab -l > $cron_file
echo "$@" >> $cron_file
crontab $cron_file
sleep 100000000

(Hikmat Ullah) #6

I need in docker run command.
Already have in Dockerfile

thanks


(Sam) #7

then the docker run would be

docker run -d image_name '*/5 * * * * ___cmdstring___'

where cmdstring is the oneliner for the commands to be issued… (with quotes escaped as required)

my test Dockerfile is

from ubuntu
run apt-get update
run apt-get install cron ack-grep
add ./startup.sh .
entrypoint ["/startup.sh"]

(Hikmat Ullah) #8

When I run below command, cron job is not adding!

docker run -d hikmat30cd/schedulewebjob ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’


(Sam) #9

AND u need the startup.sh entrypoint command to build the crontab file, right?

if you docker exec container_id cat /tmp/cron_file what do you see, also you can docker exec crontab -l to see if it was added…

had to fix the dockerfile to add grep to the image


(Hikmat Ullah) #10

I got executed below command successfuly, now converting it to json object as I have to call it from code. Can you help me in that?

docker exec mydocker bash -c “echo ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’>> /var/spool/cron/crontabs/root”


(Sam) #11

what does ‘call it from code’ need in the json object?


(Hikmat Ullah) #12

Yes, I need to call it in php


(Sam) #13

no… in the json object, what fields are required? named? anonymous, array, structure?

what php command are you using?


(Hikmat Ullah) #14

If you provide me json object that will enought,
by the way I am using below library
docker-php


(Sam) #15

ok, setCmd method takes an anonymous array ([…,…,…])

$containerConfig = new ContainerConfig();
$containerConfig->setImage(imagename);
$containerConfig->setCmd([“bash”, “-c \“echo ‘*/5 * * * * /usr/bin/php /app/yii cronrun/runjob’>> /var/spool/cron/crontabs/root\””]);
containerCreateResult = $containerManager->create($containerConfig);


(Hikmat Ullah) #17

Thanks @sdetweil for your help.
it worked by following below steps
1 create exec instance
2 start exec instance