How to pass crontab command when running docker

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

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
1 Like

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

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

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
1 Like

I need in docker run command.
Already have in Dockerfile

thanks

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"]
1 Like

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

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

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

1 Like

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”

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

Yes, I need to call it in php

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

what php command are you using?

1 Like

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

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);

1 Like

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