How to invoke cron job from php script?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 10:25:13

问题


I wanted to set cron job from php script file. I can able to execute php file using shell_exec() function. But Im not able to run cron job related commands. $output = shell_exec("crontab -l"); this command is not working. My cronjob located under /usr/bin/crontab. I set the file premission to 777 and im executing this command with root access. still no luck. can anyone help me?


回答1:


Your "crontal -l" command just displays what's scheduled for your user in its personal crontab. It might return an empty string, depending on your current personal crontab. Don't mix up with the file /etc/crontab which is a system-wide crontab, for all users, writable by root only.

If your need is - as I think I understood - to add a job in the crontab from a php script, maybe you might simply want to try something like :

$r=shell_exec('cat "30 6 * * * user my_cmd my_args" >> /etc/crontab');

To schedule "my_cmd my_args", running as "user", 6:30 am every day, for example. This PHP script should be started as root as only him can write in /etc/crontab.

Careful : I hope your php script is not started from a web site, but in command-line from an access-restricted environment, to limit security risks, especially if you do something to make it start as root. That kind of script is a very big hole in your system. Do consider this. That's my advice.

By the way, /usr/bin/crontab's permissions back to :

-rwxr-sr-x 1 root crontab 35040 19 déc. 2010 /usr/bin/crontab (example from a Debian system).



来源:https://stackoverflow.com/questions/10511996/how-to-invoke-cron-job-from-php-script

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