Send nodejs script to background in php without blocking - php

I have a nodejs script which opens a torrent stream which I then use to grab screenshots from. Currently I use a bash script for this which is not really extensible. I decided to move most of this to php by using a handy FFMpeg wrapper for php.
The problem is that I can't seem to initiate the nodejs script from within php without blocking. This is the command I'm running using exec()
node ~/node/peerflix/app.js 'test.torrent' &> /dev/null &
However whatever I try, the script hangs on exec. I just want to send it to the background. In other words, how can I asynchronously execute a nodejs script and send it to the background without caring about the output.
It would also be nice to be able to get the PID of the process so I can kill it when I'm done
I've been doing some research on ReactPHP and perhaps that would be a solution but I have no idea how to get that going.

Let me suggest something:
Use forever for manageable running.
You can use it in a javascript runner script of as a bash (my favor)
npm install forever -g
forever start ~/node/peerflix/app.js 'test.torrent' //in bash
This will run app.js forever, and can be monitored with the forever cli

Related

Get running status of python program in PHP

Hi I am doing a project with Raspberry Pi. I make a python program which have an endless loop inside. I also make a PHP website which call to that python program, make it run in background by this way:
$call = "sudo python ../python/readver12.py > /dev/null 2>&1 &";
shell_exec($call);
Everything seem okay, but I don't know how to get the status of my python program is running in background or not, and make it available in my website with PHP ?
I guess there is many ways to do that:
Try get the logs from terminal
You can throw some logs at your terminal while running your endless python script, and capture it with PHP
https://docs.python.org/3/howto/logging.html
Write on files and read with it with php
Straight forward, you write in file with python and read with PHP
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
API REST On PHP website and python with cURL
You can use cURL inside your python script to comunicate with your endpoints in php and get the needed data.
cURL: http://pycurl.io/docs/latest/index.html
PHP Api Rest: https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
I hope it helps
With shell_exec() PHP will wait to continue your script until the application you're calling exits and will return the output of that application as a string. When your application picks back up the child is already done (or you've hit the PHP time limit).
It sounds like you want to start the process and monitor it while it's running. For that look at proc_open() and proc_get_status().

How to run PHP script as a background service on AWS?

Now I am trying to execute php websocket script as a background. For it, I used below command on the AWS terminal.
php chat-server.php >> log.txt &
But strangely it is terminated without any errors.
My question is two.
Why the php command is terminated on AWS? Is there any limitation to run a php script continually as a background service
How can I run it permanently on AWS? I understand I can use a linux script to run my web-socket server permantely (maybe linux script will be re-lunch my php script when it will be terminated). But I don't know well linux commands. Who can help me?
Thanks for your advice!
First, enable PHP logs to see if there are any errors.
Then, to run it permanently, you can use screen or Tmux. It's simple to use and lets you detach the process and run it in the background. So you are able to logout from your ssh session, and the process is still running.
Please read:
https://wiki.archlinux.org/index.php/tmux
http://linux.die.net/man/1/screen

How do I get the progress of an rsync run by a php exec command

I have a shell script (rsync.sh) with an rsync command in it:
rsync -rtlv --password-file=/path/to/password/file/file.txt --bwlimit=5000 /local/root/path/$1 username#remoteserver::remote/root/path/$2
I then run that from PHP (rsync.php) with an exec command:
exec('/path/to/shell/script/rsync.sh local/specific/path/ remote/specific/path/', $progress, $errors);
This all works fine. I get the progress once it's finished and I parse it. So far I've only been testing on a few smaller files. However, once this is put into production I am expecting this to be done on several large files that will take over an hour to finish. I would like to be able to view the progress as it's happening. If I put the --progress flag in there I'm not sure exactly how I'll get the progress back through the exec. Any ideas?
I think I'll have to make the exec asynchronous and somehow post the progress to a database where it can be collected and displayed.
You can't. The PHP exec() function does not return until the command completes.
This is really not a very good task for PHP — web servers generally do not cope well with PHP scripts (and, hence, web requests) that take hours to complete. That being said, you will be able to get closer to the intended behavior using the proc_open() family of functions, which allow you to start a process that runs in parallel with a PHP script.

How to run a background process in PHP and read/write its stdout/stdin?

As an extension to question "php execute a background process":
Suppose I wanted to keep a process running during a PHP session, like an interactive bash shell. How can I establish redirection of stdout/stdin such that PHP can read/write to the process?
UPDATE: The key is to be able to kick off the process and keep it running in the background so that later requests can access its stdout/stdin streams.
I would use PHP Expect. The documentation has some very good usage examples.
If you're using Linux, you can access the proc file system at /proc. Though distributions may differ somewhat, in Ubuntu Server I can find my stdio at /proc/<pid>/fd/[012]. 0 is stdin, 1 is stdout, and 2 is stderr. This will probably not work if you are redirecting these from or to /dev/null, as some methods of spinning off long running background processes have you do. If you have access to the pid of the background process (a la http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/), you should be able to access the stdin / stdout of the process in Linux.
If you're using PHP to relay user-typed commands to a shell, you can use mulitple calls to shell_exec
shell_exec will return the complete output which you can then echo back to the user.

running console app from php script

i start a linux console app from my php5 script, it starts ok but then termintates. I've tried using system(), shell_exec and tried starting as background process but to no avail it starts and then quits.
What i am trying to achieve is from a remote browser start a console app using a php5 script and then it should remain running (just as it would if i started it from a bash shell) , i then want to send commands (from a bash shell it would be keyboard strokes) to the console app from another set of php5 scripts. Hope its clear what i am trying to do.
If anyone could give some info on the best way about doing this, as i think i may have something fundamentally wrong.
I have a Debian Lenny box running apache.The console app is just a simple program that prints to stdout and reads from stdin.
How do you expect to send input to this app? Where is it listening for input?
It simply may only support interactive use, and exit as a result of that. Or, even simpler, it may terminate because it sees that is has no input (nothing piped in or nothing from some file) and since it's not connected to an interactive shell, it has nothing to do. There's no point in waiting for input from a user that doesn't have a way to interact w/ the application.
On every request, PHP starts up, compiles your script and executes it. After execution, the script exists. When the script exits, all of the resources it was using, including file handles, database handles, and pipes to other programs are terminated.
You're going to need to find another way to keep your program open and have PHP communicate with it. Otherwise, every request to your script is going to open a new copy of the program, and then both will exit when the PHP script is complete.
Unfortunately without knowing what the program is, it will be hard to offer suggestions on how to go about doing this.

Categories