crontab不执行Docker中的命令

发布于 2025-01-27 00:35:50 字数 1082 浏览 2 评论 0原文

我正在建立一个网络应用程序,讲师为课程提供课程,客户购买它们。 我需要自动化以更改课程状态。几乎只是检查某些条件,例如当当前时间大于截止日期时,例如关闭课程。 我正在使用php-symfony,一切都被停止了。因此,要访问我的PHP容器,我必须启动Docker-Compose并键入Docker-Compose Exec PHP Bash。在PHP中,我有一个可以手动运行的命令,检查所有课程状态,如果满足条件,可能会对数据库进行一些更改。但是为了自动化此过程,我决定使用crontabs。我已经在CMD中测试了该命令,无论我是否可以访问此类操作并且有效,所以我只是将其放入crontab中以每分钟执行。

* * * * * cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1

第一部分到达项目文件夹,第二部分打开Docker容器并运行检查课程状态的命令。在CMD中,整个命令工作。甚至在/var/log/syslog 中,它应该是默认的crongab记录文件,它也给了我这个输出:

May  8 16:42:01 martin-ubuntu CRON[1032921]: (martin) CMD (cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1)

我看不到任何可见错误,但是在数据库中查看在应该的情况下更改任何值。

所以我认为这可能和Docker在一起吗? 我试图在Crontab中添加第二个命令,即

* * * * * cd /home && echo "hi" > a.txt

每分钟都将“ HI”保存到给定文件中。 因此,尽管第一个命令不起作用,但这确实如此。有什么想法隐藏的问题在哪里?

I'm building a web app where instructors provide courses and customers buy them.
I needed automation for changing courses statuses. Pretty much just checking some conditions and e.g. closing course when the current time is greater than deadline.
I'm using php-symfony, everything is dockerized. So to access my php container, I have to start docker-compose and type docker-compose exec php bash. Well in php I have a command which I can run manually, to check all the courses statuses and possibly do some changes to database if the conditions are met. But to automate this process, I decided to use crontabs. I've tested the command in cmd whether I have all the access to do such thing and it worked, so I just put it into crontab to execute every minute.

* * * * * cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1

The first parts gets to the project folder, the second part opens docker container and runs the command that checks courses statuses. In cmd the whole command worked. And even in /var/log/syslog which is supposed to be default crontab logging file, it gave me this output:

May  8 16:42:01 martin-ubuntu CRON[1032921]: (martin) CMD (cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1)

I don't see any visible errors but looking in the database, it didn't change any values while it should.

So I was thinking it could be something with docker?
I tried to add second command in crontab which is:

* * * * * cd /home && echo "hi" > a.txt

And it was saving "hi" into given file every minute.
So while the first command doesn't work, this one does. Any ideas where is the hidden problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-02-03 00:35:50

将您的crontab更改为

* * * * * sudo docker-compose -f /home/martin/PhpstormProjects/bp_project/docker-compose.yml run --rm php sh -c ' bin/console courses-check' >/dev/null 2>&1

change your crontab into this

* * * * * sudo docker-compose -f /home/martin/PhpstormProjects/bp_project/docker-compose.yml run --rm php sh -c ' bin/console courses-check' >/dev/null 2>&1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文