Running a MATLAB Script on an already running MATLAB Session - php

I am using a PHP server at the back end and a basic web page which asks the user to upload an image. This image is used as an input to a MATLAB script to be executed on the server side.
What I need is something like a MATLAB session(not clear on that word) that is already running on the server side which runs the MATLAB script. I know about the command: "matlab -nodesktop -nojvm" which can be used but the point is that I don't wish to invoke MATLAB again and again, rather, just execute the MATLAB script on the running MATLAB instance whenever a user uploads an image, get the output(necessary).
There are some constraints:
1. OS -> Ubuntu
2. Can't use python engine.

There exist multiple interfaces to control matlab. Probably best choice for this case is matlabcontrol or the matlab engine for python (which you can't use for some reason). On windows a third alternative would be com.
Besides controlling the matlab process, you could implement an application in matlab which recieves the data, processes it and sends it back. I solved a similar problem using apache xmlrpc in matlab.
There are also some submissions on matlab file exchange, directly providing a matlab console via web

You could write Matlab code to check the upload folder for new images regularly. Process new images and then move the processed images to an archive folder.
To check for new files use dir command
FILES = dir(['path/to/upload/folder/*.PNG']);
Replace PNG extension with that of your image files.
To move files use movefile command
movefile('path/to/upload/folder/Filename.PNG', 'path/to/archive/folder/', 'f')
To run the Matlab script from terminal and keep it running in the background
/usr/local/MATLAB/R2014a/bin/matlab -nodisplay -nosplash -r "cd /path/to/matlab/code; MatlabScript" ; < ctrl > Z; bg; disown -h %1

There is an API for C++ where you call the Matlab engine using engOpen. This will open Matlab and leave it running until you close it. Then your C++ program can wait and listen for the image to process.
http://www.mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html
Another option is to compile the Matlab script as a standalone executable. Hard code the input image name and output and let PHP handle moving around the inputs and outputs. All the server needs to do is call the executable. It takes about 5 seconds to start the Matlab runtime each time.

Related

Why Php FFmpeg exec() process stop my web app?

I have my web app developed on azure. When I start an ffmpeg process with exec, if the file is larger than 50MB, it stops the web app until it finish to process the file. Why does that happens?
Funny enough I programmed this for a customer myself some weeks ago.
You can use AJAX in front-end and start each convertion via a JS.
You can use a php "daemon"
2.0 Start the program independently php converter.php video1.mp4 >/dev/null 2>&1 &
2.1 You can start the normal script (via website), and it could restart itself as independent php converter.php (e.g. when a POST or GET is given) and the program will start itself in the script with shell_exec('php converter.php daemon'); (for example) in the background. You then can check via AJAX or maybe a websocket if the process has finished.
Tips:
Use a .lock-file or something similar in case the program could start a second time without it being finished yet.
Use this repo composer require php-ffmpeg/php-ffmpeg https://github.com/PHP-FFMpeg/PHP-FFMpeg

Executing a batch file on a remote server

I have an IIS server with Php and have been trying to execute scripts using exec() and system() with no luck. When i go to the machine and run it by double clicking it, or by opening command and running the batch file, 4 windows pop open with ffmpeg streams going and dump to folder.
With exec("cmd.exe 'c:\inetpub\wwwroot\estf\batch\srRecord.bat'");
I am getting some of the batch file process working (creates new folder and dumps to file, but i can't see the cmd gui anywhere. And my stop.bat won't taskkill the ffmpeg process.
Is there a way to bring up the cmd GUI on the external device, also a way to run the exec() command and pass on so that other devices or batch files can kill these tasks (i am thinking that because the php is still running it, nothing else has rights to stop it.
Regards,
Mitchell

Can I call a function when server restarts or shutdown?

I am developing a in house app which is accessed from multiple clients.There are few xml files on the server which need to be recreated every time whenever server starts.
My question is how can I check whether the server is shut-downed or restarted via my php program? which seems abit tricky..;-) so can i run a method on server shutdown?
I am using codeigniter.
Thanks,
On systems that support cron(8) (Linux, xBSD, etc), you can use #reboot feature to execute command after reboot. Syntax for crontab is simply:
#reboot /path/to/command
You can create a batch file which takes care of creating xml files.
And place that batch file in windows start up folder. So that every time machine starts that batch file will be executed and xml files will be generated.
In batch file call your application which generates the xml files.
what system are you on, Linux?
i would use a script called over PHP Cli that is called when the system starts.
References:
on PHP Cli
http://www.php.net/manual/en/features.commandline.usage.php
on RC scripts - called when the system starts:
http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts
you can find more if you just google for those.
also like the #reboot solution, that plaes proposed.
here some examples
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

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.

Run (remote) php script from (local) python script

How do I make python (local) run php script on a remote server?
I don't want to process its output with python script or anything, just execute it and meanwhile quit python (while php script will be already working and doing its job).
edit:
What I'm trying to achieve:
python script connects to ftp server and uploads php script (I already have this part of code)
it runs php script (that's part of code i'm asking about)
python script continues to do something else
python script quits (but probably php script still didn't finished its work so i don't want it to end when it'll exit python)
python script quit, php script still continues its task
(I don't plan to do anything with php output in python - python just has to upload php script and make it start working)
Hope I'm more clear now. Sorry if my question wasn't specific enough.
another edit:
Also please note that I don't have shell access on remote server. I have only ftp and control panel (cpanel); trying to use ftp for it.
os.system("php yourscript.php")
Another alternative would be:
# will return new process' id
os.spawnl(os.P_NOWAIT, "php yourscript.php")
You can check all os module documentation here.
If python is on a different physical machine than the PHP script, I'd make sure the PHP script is web-accessible and use urllib2 to call to that url
import urllib2
urllib2.urlopen("http://remotehost.com/myscript.php")
I'll paraphrase the answer to How do I include a PHP script in Python?.
import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path] )

Categories