How to set a crontab using php?

那年仲夏 提交于 2019-12-06 11:36:10

问题


i need to set a crontab from a php script. i know that it can be set by

system("echo '* * * * * echo \"Hello world\"' >> cron.crontab"); system("crontab cron.crontab"); //cron.crontab is the file name of the crontab

"cron.crontab" file is created in root directory, but crontab is not working as expected! when i try the following command, it says that no crontab is set!

crontab -l

i think i am missing something. i am using yii framework. is there an extension to handle cron in yii framework? is there any other way to do it? Please help me. Thanks.


回答1:


Are you running crontab -l as the same user as the web server is running under? Otherwise you won't see its cronjobs.




回答2:


Apache generally isn't going to be able to muck around with the main crontab. However, each user has a crontab as well, and it's quite likely that your code above is setting apache's crontab (or the crontab for whatever user your site runs as).

Try crontab -u apache -l, or replace apache with whatever user your site runs as, and see if the entries you set are there.




回答3:


You generally have to be root to set the crontab, and it's unlike that php is running as root, in which case you're out of luck.

Edit: Not completely out of luck. See the link in Sebastian's comment above.



来源:https://stackoverflow.com/questions/5051464/how-to-set-a-crontab-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!