大网数据广告活动
服务器运维问答

How to check if php built-in server is running

· 52CE 我爱测

Recent versions of PHP has a built-in webserver (for non-production environments) that can be started with php -S [...] . How can I check if it's already running on the server before attempting to start it? I realise that the hac…

问题原文

原帖链接 · jgivoni · CC BY-SA 3.0

Recent versions of PHP has a built-in webserver (for non-production environments) that can be started with php -S [...].

How can I check if it's already running on the server before attempting to start it?

I realise that the hacky way would be to try to start it and look out for the error message Failed to listen on [...], but I thought there might be a cleaner way :-)

EDIT: I forgot to mention that ultimately I'd like to be able to check this in a script and only start the server if it's not already started. I also forgot to mention that I'm looking for a solution for Linux, but Windows solutions are of course welcome as well, in case someone else will be looking for that one day :-)

已采纳解答

原帖链接 · jgivoni · CC BY-SA 3.0

Since my edit to Paolo's answer was rejected, here's a more complete answer:

ps aux | grep php

This command will output something like the following if php is currently running:

pi        3245  0.0  0.5  21400  2416 pts/0    S    Jan29   0:02 php -S localhost:8000 -t /path/to/php/docroot
pi        3518  0.0  0.3   5220  1712 pts/0    S+   00:42   0:00 grep --color=auto php

The second line can be ignored as it's just self-referencing the search command. This script will check if the server is running and start it if it's not:

if ! ps aux | grep -v grep | grep -qs 'php -S localhost:8000';
then
  php -S localhost:8000 -t /path/to/php/docroot &>/dev/null &
fi

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。