1

I have some PHP files. Each of them start a socket listener, or run an infinite loop. The scripts halt when are executed via php command:

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. 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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
MarcusM
  • 33
  • 6

2 Answers2

2

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.

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;
  • -t sets the window title.

The session will be available in the output of screen -ls command:

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

Connect to the session using -r option, e.g.:

screen -r 8951.php-test

List the windows within the screen session with Ctrl-a " shortcut, or windowlist -b command.

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. Read this tutorial for more information;
  • 2>a.php.err redirects the standard error to a.php.err file;
  • >a.php.out redirects the standard output to a.php.out file.

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?

You can put the above-mentioned commands into a shell script file, e.g.:

#!/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.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • Thank you for your detailed answers, question modification as well. – MarcusM Jan 09 '17 at 08:27
  • I put 2 screen commands into a shell script and call it, the second screen line is not reached because that it stucks at the first screen session, I have to press ctrl+a-d, then the second screen line is started. While I need a shell script which can open 2 screen sessions just by calling it, no need additional operations. Do you have any ideas? Thanks! – MarcusM Jan 12 '17 at 01:57
  • @MarcusM, the shell script should look like [this](https://gist.github.com/a0044b80f280b8cd3b6cde2632ed6f58) (_three_ `screen` commands). When you launch it: `./script.sh`, it doesn't forward you to the screen session, since it is detached. – Ruslan Osmanov Jan 12 '17 at 02:02
  • Excuse me. The network cannot access github, my vpn proxy doesn't work today. Could you please paste the content of the this link page? – MarcusM Jan 12 '17 at 03:10
  • Cherring! It works. The shell script finally looks like this: `#!/usr/bin/bash` `nohup php myfile1.php 2>path/myfile1.err >myfile1.out &` `nohup php myfile2.php 2>path/myfile2.err >myfile2.out` `` `echo "Startup done."` `exit` By the way, if the sh is edited in vi, I have to set ff=unix. Besides that, I have no idea about the '&' symbol in the .sh. When should I attach it to the end of the line. Anyway, Thank you very much @Ruslan Osmanov. You helped me a lot. – MarcusM Jan 12 '17 at 06:44
  • @MarcusM, the `&` symbol sends the preceding command to background so you can proceed without the need to wait until the command finishes. `nohup` makes sure that the command will continue running, if you log out . If you think that the answer solves the problem, consider accepting and/or upvoting it – Ruslan Osmanov Jan 12 '17 at 07:12
  • Of course it do solve the problem. Stackoverflow is so good. Thanks a lot. But Sorry...where is the accepting btn? – MarcusM Jan 13 '17 at 04:55
  • I've accepted your answer, right? Can I ask another question? You said the & symbol makes it proceed without waiting the command to finish. What makes me confused is that the `make install` is not proceed until `make` is finished when we type `make&make install` to install sth. I've always thought the & symbol just queue up several commands, while they are proceed one by one – MarcusM Jan 13 '17 at 05:13
  • @MarcusM, you are confusing single ampersand (`&`) with double ampersand (`&&`). The latter is a logical _AND_ operator that executes the right-hand command, if the left-hand command returns _true_ (i.e. terminates with exit status zero). Thus, `&&` works synchronously, and the right-hand command is executed only after the left-hand command is terminated (successfully). – Ruslan Osmanov Jan 13 '17 at 06:40
  • Shamed. I was supposed to be more carefully. – MarcusM Jan 13 '17 at 07:07
0

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/

Jesse Weigert
  • 4,714
  • 5
  • 28
  • 37