从php创建cron [英] creating crons from php

查看:144
本文介绍了从php创建cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PHP中创建cron工作,有点像cPanel或Plesk,但我被卡住了。我想这个程序,不强迫用户去cPanel手动创建crons

I want to create cron jobs from within PHP, a bit like in cPanel or Plesk but I'm stuck. I want to do this programatically and not force the user to go to cPanel to create the crons manually.....

尝试了很多东西,读了很多东西...包括这里stackoverflow,没有什么工作。
我尝试编辑crontab从php像shell_exec('crontab /pathtomycronfile/cron.txt'),然后编辑cron文件本身,但它不工作。用户是'apache',我尝试设置crontab apache ..不工作...

Tried many things, read many things... including here on stackoverflow, nothing works. I try editing crontab from php like shell_exec('crontab /pathtomycronfile/cron.txt') and then editing the cron file itself but it doesn't work. User is 'apache' and I tried setting the crontab for apache .. doesnt't work either ...

任何人都可以帮助这个?

Please can anyone help with this? Have you been able to create cron jobs from php before?

推荐答案

在某些* nix系统下,crontab会存储每个用户特定的cron文件/ var / spool / cron / crontabs / [USERNAME]。

Under some *nix systems, crontab will store each user-specific cron file in e.g. /var/spool/cron/crontabs/[USERNAME] . They're not intended to be edited directly, but you could get PHP to do so.

您可能还需要更改包含目录的权限,以便PHP可以直接编辑可以看到文件要编辑它。然后您可以使用例如:

You might also need to change the permissions on the containing directory, so that PHP can see the file to edit it. You could then do so with e.g:

<?php
$user = get_current_user();
$cron_file = fopen("/var/spool/crontabs/$user", "a");
fwrite($cron_file, "\n* * * * * touch /tmp/testcron\n");
fclose($cron_file);

说实话,这是非常危险的。相反,你应该考虑有一个cron.php,每15分钟调用一次,并把所有的逻辑关于什么实际任务应该在一个给定的季度,在cron.php中进行。不要编辑crontab将逻辑放在那里。

To be honest, though, this is very dangerous. Instead, you should consider having a cron.php, which gets called every 15 minutes, and put all your logic about what actual tasks should be carried out at a given quarter-hour in that cron.php. Don't edit the crontab to put the logic in there.

这篇关于从php创建cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆