Home > database >  docker: command not found if called in a cronjob
docker: command not found if called in a cronjob

Time:06-07

On my server (QNAP) I can run docker:

$ docker -v
Docker version 20.10.11-qnap6, build 90a753c

If I want to run the same docker command in a crontab

$ sudo crontab -e
* * * * * docker -v >> /root/cron.log

I do get the error log

/bin/bash: docker: command not found

I do not understand why docker cannot be used as a crontab job, but it works, if I run it directly.

CodePudding user response:

You should first run:

whereis docker

Mine is:

/usr/bin/docker

Then modify crontab file as below:

* * * * * /usr/bin/docker -v >> /root/cron.log

You also can change crontab file like this (above your crontab commands):

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

The complete sample in your case:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * docker -v >> /root/cron.log
  • Related