我正在尝试将新文件添加到我的ubuntu服务器上的/etc/cron.d/
目录中,以便在添加新用户时添加/运行新的cron脚本。用户是通过Web UI创建的,因此PHP代码由用户www-data
执行,但由于/etc/cron.d/
目录由root
拥有,我无法在那里写。
我确实控制了整个服务器,但出于安全原因,我不想永久地将/etc/cron.d/
更改为www-data
所有。
理想情况下,我想在一个PHP文件中完成所有操作,但我尝试使用外部bash脚本来导航权限,但没有运气。从本质上讲,这就是我想要做的工作:
$filename = "something.cron";
file_put_contents("/etc/cron.d/".$filename, "* * * * * /usr/bin/php -f /another/file/to/execute.php");
最好的方法是什么?我很抱歉,如果已经被问到/已经回答过,我搜索过并发现了十几个类似的问题,但没有什么比得上since it's been over 30 minutes, I figured I could ask for help。
答案 0 :(得分:0)
您需要使用Cron
命令设置exec
作业,并且无法使用PHP
设置
exec('echo -e "`crontab -l`\n30 11 * * * /path/to/script" | crontab -');
这将每天11:30运行脚本。
查看本教程:
http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428