执行 bash 命令有效但在 crontab 上无效

execute bash command works but not on crontab

我正在使用 aws lightsail 产品。

当我执行 bash 推荐时,

* * * * * docker exec -it my_container_name Rscript /home/path/to/file.R

我还使用

检查了 cron 日志
grep CRON /var/log/syslog

它说它工作正常。 但是没有给出结果

为什么 运行 -它 ?

把脚本放在容器里然后运行 -d不是更好吗?

除此之外,如果您使用 cron 而不是交互式登录,您的 bash-env 可能设置不同。 PATHS 、 LC 设置给我带来了大部分问题。

你的dockerimage文件有没有ENTRYPOINT

copy run2.sh /usr/local/bin/run2.sh
run chmod +x /usr/local/bin/run2.sh
ENTRYPOINT ["/usr/local/bin/run2.sh"]

您的 run2.sh 文件应如下所示(因此它可以响应来自 OS 的信号,即关闭)

#!/bin/bash
#This is a run file - which should be used to add things you want your
#container to do when you start it up
#
#If you do not have this then ...
#  docker-compose up ---- simply exists as there is nothing for the container to do.

trap cleanup 1 2 3 6 9 15

cleanup()
{
  echo "Caught Signal ... cleaning up."
  service postgresql stop
  sleep 1s
  echo "Done  ... quitting."
  exit 1
}

service postgresql start
# wait forever
echo "Starting up"
while true
do
  tail -f /dev/null
done

注意: 这消除了启动服务的必要性,而且您可以指定所需的关闭步骤。

如果你愿意,你可以有多个规则,我只是懒惰,把它们都放在一起。

希望这对您有所帮助....