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?

1
  • don't use exec command Commented Jan 9, 2017 at 2:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your detailed answers, question modification as well.
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, the shell script should look like this (three screen commands). When you launch it: ./script.sh, it doesn't forward you to the screen session, since it is detached.
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?
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.
|
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.