大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 containe…

问题原文

原帖链接 · ewramner · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · Matt Zimmerman · CC BY-SA 4.0

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:[~] 

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。