如何从php脚本调用cron作业? [英] How to invoke cron job from php script?

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

问题描述

我想从php脚本文件设置cron作业。我可以使用shell_exec()函数执行php文件。但我不能运行cron工作相关的命令。 $ output = shell_exec(crontab -l); 此命令不工作。我的cronjob位于/ usr / bin / crontab下。我将文件预先设置为777,并以root访问权限执行此命令。仍然没有运气。

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?

推荐答案

您的crontal -l命令只显示您的用户在其个人crontab中排定的内容。它可能返回一个空字符串,取决于您当前的个人crontab。
不要混淆文件/ etc / crontab,这是一个系统范围的crontab,对于所有用户,只能由root写入。

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.

需要的是 - 我想我的理解 - 添加一个工作在crontab从PHP脚本,也许你可能只是想尝试像:

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');

要安排my_cmd my_args,以user运行,每天上午6:30例。
这个PHP脚本应该以root身份启动,因为只有他可以在/ 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.

注意:我希望你的php脚本不是从网站启动,但在命令行从限制访问的环境,以限制安全风险,特别是如果你做某事,以使其作为根启动。这种脚本在你的系统中是一个很大的洞。请考虑这一点。这是我的建议。

顺便说一下,/ usr / bin / crontab的权限回到:

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

-rwxr-sr-x 1 root crontab 35040 19déc。 2010 / usr / bin / crontab
(来自Debian系统的示例)。

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

这篇关于如何从php脚本调用cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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