简体   繁体   English

如何在单个shell行中执行多个PHP文件?

[英]How do I execute multiple PHP files in single shell line?

I have some PHP files. 我有一些PHP文件。 Each of them start a socket listener, or run an infinite loop. 它们每个都启动套接字侦听器或运行无限循环。 The scripts halt when are executed via php command: 通过php命令执行时脚本停止:

php sock_listener.php ...halt there
php listener2.php ... halt there
...

Currently I use screen command to start all the listener PHP files every time the machine is rebooted. 当前,每次重新启动计算机时,我都使用screen命令启动所有侦听器PHP文件。 Is there a way I can start all the listener PHP files in single shell line so that I can write a shell script to make it easier to use? 有没有一种方法可以在单个shell行中启动所有侦听器PHP文件,以便编写shell脚本以使其更易于使用?

Using screen 使用画面

Create a detached screen session for the first script: 为第一个脚本创建一个独立的屏幕会话:

session='php-test'
screen -S "$session" -d -m -t A php a.php

where -d -m combination causes screen to create a detached session. -d -m组合会导致屏幕创建一个分离的会话。

Run the rest of the scripts in the same session in separate windows: 在单独的窗口中的同一会话中运行其余脚本:

screen -S "$session" -X screen -t B php b.php
screen -S "$session" -X screen -t C php c.php

where 哪里

  • -X sends the built-in screen command to the running session; -X内置的 screen命令发送到正在运行的会话;
  • -t sets the window title. -t设置窗口标题。

The session will be available in the output of screen -ls command: 该会话将在screen -ls命令的输出中可用:

There is a screen on:
  8951.php-test (Detached)

Connect to the session using -r option, eg: 使用-r选项连接到会话,例如:

screen -r 8951.php-test

List the windows within the screen session with Ctrl - a " shortcut, or windowlist -b command. 列出使用Ctrl屏幕会话中的窗口- 一个 快捷方式,或windowlist -b命令。

Forking Processes to Background 将过程分叉到后台

A less convenient way is to send the commands to background by appending an ampersand at the end of each command: 一种不太方便的方法是通过在每个命令的末尾附加一个&符来将命令发送到后台:

nohup php a.php 2>a.php.err >a.php.out &
nohup php b.php 2>b.php.err >b.php.out &
nohup php c.php 2>c.php.err >c.php.out &

where 哪里

  • nohup prevents termination of the commands, if the user logs out of the shell. 如果用户退出外壳, nohup阻止命令终止。 Read this tutorial for more information; 阅读本教程以获取更多信息;
  • 2>a.php.err redirects the standard error to a.php.err file; 2>a.php.err将标准错误重定向到a.php.err文件;
  • >a.php.out redirects the standard output to a.php.out file. >a.php.out将标准输出重定向到a.php.out文件。

Is there a way I can start all the listener PHP files in single shell line so that I can write a shell script to make it easier to use? 有没有一种方法可以在单个shell行中启动所有侦听器PHP文件,以便编写shell脚本以使其更易于使用?

You can put the above-mentioned commands into a shell script file, eg: 您可以将上述命令放入一个shell脚本文件中,例如:

#!/bin/bash -
# Put the commands here

make it executable: 使它可执行:

chmod +x /path/to/script

and call it when you need it: 并在需要时调用它:

/path/to/script

Modify the shebang as appropriate. 适当修改shebang

Just run them under circus. 只是在马戏团下经营他们。 Circus will let you define a number of processes and how many instances you want to run, and just keep them running. 马戏团可以让您定义多个进程以及要运行的实例数,并保持它们的运行状态。 https://circus.readthedocs.io/en/latest/ https://circus.readthedocs.io/en/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM