Ubuntu 18.04 cron job calling docker silently fails in run-parts but works in plain crontab

crondockerUbuntu

I have copied several cron jobs from a CentOS 7 server, where they have been running in cron.daily, to a new Ubuntu 18.04 server. The jobs call into docker containers and look like this:

#!/bin/bash
/usr/bin/docker exec containername scriptname.sh

The scripts are executable and work correctly when invoked by root from a shell:

/etc/cron.daily/script.sh # this works

Furthermore when I invoke the scripts directly from /etc/crontab they work:

35 0   * * *   root    /etc/cron.daily/script.sh # this works

However, they should be invoked by run-parts:

45 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

This silently fails both when the script is in cron.hourly and cron.daily. The syslog contains lines like:

Jul 17 09:45:01 servername CRON[5436]: (root) CMD (   cd / && run-parts -v /etc/cron.hourly )

In other words run-parts is called. Other scripts in cron.hourly and cron.daily that are not calling docker seem to work. There are no error messages that I can find. On CentOS I would have guessed that this was a SELinux issue, but here I'm stumped. The scripts are executable (or they would not have been callable from crontab), the docker client is not interactive and does not use the terminal (no -it).

What could be wrong?

Note that I do have a perfectly good workaround here. I can simply call the jobs from crontab, problem solved. I'm posting as I want to understand why this doesn't work when the scripts are invoked by run-parts so that I can avoid making a similar mistake in the future.

Best Answer

Try removing the .sh extension from your script:

wally:[~] ls -l /tmp/foo      
total 8
-rwxr-xr-x 1 mdz mdz 27 Aug  5 22:58 bar.sh
-rwxr-xr-x 1 mdz mdz 27 Aug  5 22:58 baz
wally:[~] cat /tmp/foo/bar.sh
#!/bin/bash

echo I am bar
wally:[~] cat /tmp/foo/baz  
#!/bin/bash

echo I am baz
wally:[~] run-parts /tmp/foo 
I am baz
wally:[~] mv /tmp/foo/bar.sh /tmp/foo/bar
wally:[~] run-parts /tmp/foo             
I am bar
I am baz
wally:[~] 
Related Topic