如何在 PHP 中找出特定进程仍在运行
我正在编写一个脚本,该脚本构建其他脚本的队列,并应该管理它们的启动。 管理器脚本应该知道哪个子进程已经完成,因此它可以启动在队列中等待的其他脚本。
我添加了一个“& echo $!” 获取每个子进程的进程 ID。 所以我有我的子进程进程 ID,现在正在使用系统“ps”程序调用来查找子进程是否仍在运行。
问题是我的脚本目前只能在类 Unix 系统中运行。 我不知道如何在 Windows 中获取我孩子的 PID,而且我的脚本还无法在 Windows 中解析“tasklist”命令的输出。
还有其他方法可以实现这一目标吗? 有没有 PHP 内的解决方案来查找子进程是否仍在运行? 启动其他进程(非阻塞)并检查它们是否仍在运行的解决方案。
I'm writing a script that builds a queue of other scripts and is supposed to manage their launch. the manager script should know which child process has finished, so it can launch other scripts waiting in the queue.
I added a "& echo $!" to get the Process Id of each child process. so I have my child processes Process Ids, and for now am using system "ps" program call to find if child processes are still running or not.
the thing is that my script currently runs only in Unix-like systems. I don't know how to fetch my children's PID in windows, and my script does not parse "tasklist" command's output in windows yet.
Is there any other way to achieve this? any in-PHP solution to find if the child process is still running? a solution to start other processes (non blocking), and check if they are still running or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会发现进程控制对于 Unix 环境很有趣。 您还可以在手册中的 注释中找到在 Windows 上执行程序的示例,这让我想到了 COM 对象。
You may find Process Control interesting for Unix environments. You may also find an example of executing programs on Windows as comment in the manual, and this points me to think of COM-objects.
您可以做的是创建一个数据库或文件来保存您的进程 ID。 每个进程都会将自己的pid(进程ID)写入文件或DB中。
使用此方法获取您的 php pid:
您的监督进程将时不时地检查进程 ID 是否仍在运行,并显示以下信息:
当进程停止时,您可以执行下一个进程
,对于 Windows 的使用,请检查手册中的注释: https://www.php.net/manual/en/book .exec.php#87943
What you could do is create a database or file that will hold your process ids. Every process will write his pid (process id) in the file or DB.
Use this method to acquire your php pid:
Your supervising process will check every now and then if the process id is still running with the following:
When process is stopped you can execute the next process
and for use of windows check the comment in the manual: https://www.php.net/manual/en/book.exec.php#87943
您尝试过 proc_get_status() 吗? 在这种情况下,您可能希望使用 proc_open() 生成子进程。 我不确定这是否是您要找的。
Have you tried proc_get_status() ? In that case you may want to spawn your child processes using proc_open(). Im not sure if this is what your looking for.