PHP + shell - 从 PHP 启动进程

发布于 2024-10-30 23:10:01 字数 1331 浏览 2 评论 0原文

祝大家美好的一天。

我需要这样做:

我需要从世界上某个地方的计算机访问特定的 URL 并启动一个进程。唯一的问题是,由于该过程可能会持续 5 个小时以上,并且可能会返回一些错误(该过程将移过它们),像 exec、shell_exec 等 PHP 命令不起作用(不完全知道为什么,但是......返回错误并停止执行)...所以我尝试了这个:

  • 我做了一个运行脚本的 cron
  • 使用 php 编写脚本文件

一切正常,但我需要返回一个计数器,告诉我进程何时开始,所以,当我运行编辑 shell 脚本的 php 脚本,我返回系统的小时并开始计数直到下一个整分钟(当脚本将被执行时)。

现在唯一的问题是:

计数器似乎工作正常...但该过程也需要一些时间来启动,该时间可能从 3-4 秒到 1 分钟不等。我的问题是...如果不是进程启动的确切时间以及进程启动的确切时间,我至少可以得到一些提示吗?

我不知道...就像写在一个文件中,然后一个 php 脚本读取它一分钟,看看什么时候发生了变化?

好吧...我答应我会添加一些细节:

该过程是流式传输的。居住。所以这就是我需要做的:

创建一个流 当流开始时给用户一个计数器

我使用 ffmpeg 进行流传输。 start.php 有类似这样的内容:

   // Create a file called script.sh

   $string = "ffserver & ffmpeg -i pipe.avi http://localhost:8090/feed1.ffm& echo exit 0 > script.sh& exit 0";

   //write string to script.sh
   //create a file that contains the exact time when the script was run.

cron 看起来像这样:

   * * * * * /var/www/html/script.sh > /dev/null
   and some other crons that doesn't matter

所以我可以创建一个计数器来获取脚本运行的时间,并计算到 cron 运行的下一分钟。问题是 ffmpeg 在启动之前还有一些时间(测试编解码器、检查文件等),我需要获取该时间并将其包含在计时器中,这样当流启动时,说话的人就会说“你好”。对他来说,做这样的事情不太好:“好吧……这有效吗?可以吗?你好。”并且对于用户来说失去前 3-4 句话是不好的。这就是为什么我需要 ffmpeg 启动的那一刻并以某种方式将其传递给 php 脚本。我可以从那里进行管理。

Good day to all.

I need to do this:

From a computer somewhere in this world I need to access a specific URL and start a process. The only problem is that since the process may last over 5 hours and may return some errors (the process will move over them) PHP commands like exec, shell_exec, etc don't work (don't exactly know why but... all return error and stop execution)... so I tried this:

  • I made a cron that runs a script
  • use php to write the script file

Everything works fine but I need to return a counter telling me when the process will start so, when I run the php script that edit the shell script I return the hour of the system and start counting till the next full minute (when the script will be executed).

Now the only problem is this:

The counter SEEMS to work ok... but also the process needs some time to start and that time may vary from 3-4 sec to 1 minute. My question is... can I get sometime of hint at least if not the exact time of when the process will start up and the exact time when it did start?

I dunno... like writing in a file and a php script read it for a minute to see when something changed?

Ok... I promised I'll add some details:

The process is streaming. Live. So this is what I need to do:

create a stream
give a counter to the user when the stream will start

I use ffmpeg for streaming. The start.php have something like:

   // Create a file called script.sh

   $string = "ffserver & ffmpeg -i pipe.avi http://localhost:8090/feed1.ffm& echo exit 0 > script.sh& exit 0";

   //write string to script.sh
   //create a file that contains the exact time when the script was run.

The cron looks like this:

   * * * * * /var/www/html/script.sh > /dev/null
   and some other crons that doesn't matter

So I can create a counter that gets the time when the script was run and count till the next minute when the cron will run. The thing is that ffmpeg has some time until it starts (testing codecs, checking file etc.) I need to get that time and include it in timer so when the stream starts the guy that speaks says "Hello". Is not nice for him to do something like "Ok... is this working? Yes? Hello ppl." and is not good for the users to loose the first 3-4 sentences. That's why I need the moment when ffmpeg starts and somehow pass it to a php script. I can manage from there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-11-06 23:10:01

我曾工作过 crontab 仅每 5 分钟循环一次的地方,但最近 crontab 似乎每 1 分钟循环一次。它可能是一个可配置的值,因此如果您认为 crontab 直到几分钟后才启动,这可能就是原因。

您的进程应在时间秒值从 59 移动到 00 后启动。如果某些其他进程已使用大部分/全部系统资源,启动 crontab 条目也可能会出现轻微延迟。

但从另一个角度来看,如果您正在创建具有特定时间值的 crontab 条目,那么您知道什么时间开始查看,+ 2-3 秒,对吧?

请修改您的帖子,以包含您的系统创建的 crontab 条目的示例(其中是否有“*”,这是我的主要兴趣)。


重读您的帖子,我发现您没有以编程方式创建 crontab 条目。没关系。

如果您的进程运行了很多小时,为什么您需要立即附加到它。如果它在前 30 秒内崩溃,那么最多等待 1 分钟来发现问题还不够吗?

最后,在 shell 环境中,99.99999% 的情况下,都有一种从一开始就捕获进程输出的方法。必须等待才能开始获取输出的想法需要更多解释。这与程序在远程计算机上运行有关吗?远程程序应该捕获它的输出,然后您“获取”该输出作为单独的子/协同进程。

我希望这有帮助。

I have worked at places where crontab was only cycled every 5 mins, but more recently crontab seem to be cycled every 1 minute. It is probably a configurable value, so if you don't think your crontab is starting until some multiple of minutes that may be the reason.

Your process should start after the time seconds value moves from 59 to 00. There may also be minor delays starting the crontab entry if some other process is already using most/all of systems resources.

But looking at this from another point of view, if you are creating crontab entries with a specific time value, then you know what time to start looking at, + 2-3 seconds, right?

Please modify your posting to include a sample of what your crontab entry that your system creates looks like (are there any '*' in it, being my main interest).


Rereading your post, I see you're not programamtically creating the crontab entry. Nevermind on that.

If your process is running for many hours, why you need to immediately attach to it. If it crashes in the first 30 seconds, isn't waiting 1 minute max to find that out good enough?

Finally, in the shell environment, there is 99.99999% of the time, a way to capture a process's output from the very beginning. The idea of having to wait, to start getting the output requires more explanation. Is this something todo with that the program is running on a remote machine? The remote program should capture it's output, and then you 'get' that output as a seperate sub/co-process.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文