2

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.

brainless
  • 5,359
  • 14
  • 54
  • 78
  • 3
    possible duplicate of [Use PHP to create, edit and delete crontab jobs?](http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs) – Sebastian Paaske Tørholm Feb 19 '11 at 14:55
  • 1
    Not quite a duplicate. The question involves code from the accepted answer there, which doesn't look like it's working. – cHao Feb 19 '11 at 15:15
  • The questions are slightly different, but the answer to this one is the last sentence of the accepted answer in the one linked. – Bertrand Marron Feb 19 '11 at 15:23

3 Answers3

2

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

troelskn
  • 107,146
  • 23
  • 127
  • 148
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.

cHao
  • 78,897
  • 19
  • 136
  • 168
0

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.

awm
  • 6,331
  • 21
  • 23
  • 1
    PHP runs as a user on the server...and as a user, it likely has a crontab it could set for itself. But those jobs would run as the web site/server's user, which may or may not have enough access to get the job done. – cHao Feb 19 '11 at 15:06
  • Right you are. Thanks for the clarification. – awm Feb 19 '11 at 15:08