如何从php脚本调用cron作业?

编程入门 行业动态 更新时间:2024-10-15 14:18:05
本文介绍了如何从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作业?

本文发布于:2023-11-28 18:17:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   脚本   php   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!