如何在 ubuntu 的后台运行多个 php 脚本? [英] How to run multiple php scripts in background on ubuntu?

查看:72
本文介绍了如何在 ubuntu 的后台运行多个 php 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,它应该在后台以php进程运行一些作业.同样,它应该能够识别每个进程以在以后关闭它.前任.工人1,工人2 ...如何实现的?还如何杀死被告?操作系统是ubuntu.*这些脚本始终在后台运行,因此不会被自己杀死.

I have a webpage that it should run some jobs as php processes in background. Also, it should be able to identify each process to close it later. Ex. Worker1, Worker2... How is this achieved? also how to kill the procesees? The OS is ubuntu. *Those scripts are always running in the background, so they don't get killed by themselves.

推荐答案

您可以使用开头的 nohup & 将脚本和其他Shell任务放在后台命令末尾的符号:

You can put scripts and other shell tasks in background using nohup at the beginning and the & symbol at the end of the command:

~$ nohup php script.php >> /var/tmp/script.log 2>&1 &

请注意,使用选项 2>& 1 将输出(标准错误和输出)重定向到标准输出,然后重定向到要记录的文件(此处为/var/tmp/script.log).

Note that with the option 2>&1 you redirects the output (standard error and output) to the standard output, then to a file for logging (here /var/tmp/script.log).

使用命令 jobs ,您可以在会话中列出活动的进程(此处 1797 是进程pid):

with the command jobs you can list the process you have active into your session (here 1797 is the process pid):

~$ jobs -l
[1]+  1797 Running   nohup php script.php >> /var/tmp/script.log 2>&1 &

发现进程pid后,您可以向该进程发送信号.要巧妙地"杀死一个进程(其中 $ {PID} 是进程pid):

You can send signals to the process, after you discover the process pid. To kill "nicely" a process (where ${PID} is the process pid):

~$ kill -SIGTERM ${PID}

如果进程卡住,则可以使用信号 SIGKILL (或 -9 ).请注意,无法拦截 SIGKILL ,然后该过程将立即结束,而无需执行任何清除"操作(关闭临时文件等). kill -9 $ {PID} kill -SIGKILL $ {PID} 应该仅用作最后一个资源.

If the process is stuck you could use the signal SIGKILL (or -9). Note that SIGKILL cannot be intercepted, then the process ends immediately without any "cleaning" operations (closing temporary files, etc). kill -9 ${PID} or kill -SIGKILL ${PID} should be used only as last resource.

这里有一些理论:

关于Gnu/Linux的很好的课程:

A good course about Gnu/Linux:

这篇关于如何在 ubuntu 的后台运行多个 php 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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