Communicating with Linux scripts from php web - java

I'm building a web application that helps people improve their English pronunciation for some words. The website displays a sentence for the user, and he/she speaks it and then press "Results" button. The web application then sends two files to the server: .txt and .wav files.
The server (which is Linux (Ubuntu)) should take the files, and do some analysis and calculations, and then print out the results on a file called "Results.txt". Then the web application (which is php based) should read the results from the file and displays them to the user.
The problem is: I'm not sure what is best to do the communication between the web application and the Linux server. Till now, I succeeded in writing the .txt and .wav files on the server. And I can build a Linux script that takes these two files and do the required calculations. What I'm facing is that: I don't know how to properly and effectively start the script. And more importantly: when the script is done, how to know that I can safely read the results from the "Results.txt" file? I need a synchronization tool or method.
I asked some guys, and they told me to use a java application on the server side, but I not sure how to do it!
Any help, Please?? :)

First, you can do it with PHP. Using the shell_exec() method you can run commands your Linux scripts and also read the output. Ex:
$output = shell_exec("ls -l > outputFile.txt");
will write the current directory listing to a file called outputFile.txt
Second, you can also do the same using Java. Use:
Runtime.getRuntime().exec("ls -l").waitfor();
Not using the waitfor() in the end will cause asynchronous execution of your shell script. You can also read the stdout and stderr streams of your Linux script using
Process.getInputStream()
and
Process.getErrorStream()
methods respectively. Hope that helps.

Related

Call a dockerized python script from another dockerized java application?

I need to create a Java application which sends some input parameters to a python script and sends some output back to my java application.
I cannot run the script in my java code using jython Or similar things as the python scripts are build on demand and I may need to add new scripts every now and then. So this should not impact my java app.
My java application will be running on a container and based on a few condition check it might have to select 1 of the py scripts from suppose 100 scripts and run it. And again the condition later on may change and a different script has to run at that time
I went through many websites and tutorials on the net but did not find anything relevant.
Has someone tried anything similar?

Is it possible to automatically upload files when created to a web server?

I am working on a project using a Raspberry Pi and a web cam to detect motion.
I have got it to a stage whereby it takes an image and saves it on my computer. What I am wondering is, is it possible to let FileZilla automatically upload the image to my webserver when a new image is taken? Or is there any other ways that I could achieve this?
Since the post is tagged java, I'm assuming that you're using a Java program already or have the basic knowledge to create a Java program.
On to the answer: yes, you basically have two options.
1. Upload from within the Java program. FTP is probably the easiest since most web servers will have an FTP server running. Here is a tutorial you can use: http://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example
2. Use another utility outside your Java program to upload the file to your webserver. rsync would be the tool of my choice (tutorial here). When on a Linux machine (for example, the Pi) or a Mac, you can run a script that syncs the content of a local folder to a remote folder every x seconds:
while true; do <rsync command hier> sleep 5s; done
Note that that sleep period shouldn't be too short or you'll end up running multiple instances of rsync.
When on a Windows machine, you need to find another way to run a periodic process to trigger the rsync.

Using a Java program I made on a Web Server

I created a small application that, when run, creates or updates some tables in a database by extracting data from some PDF files. Everything works fine in this desktop application, but the next step for me would be to make it possible for an administrator on a website to upload a PDF file and my Java program would then run and update the tables accordingly.
The problem is I have no idea where to start with this (the site isn't done yet, but I'm running some tests and it is going to be coded in PHP). I'd like to know what kind of technologies I need to let the server run the program and update everything as it would in the offline version. Sometimes it takes a while to update everything, so ideally, the user uploading the PDF could continue browsing other pages while the server does its job. (I'll probably implement something that when the server is done processing the file, it says if the program ended successfully or not in a log file)
Can someone tell me what terms to search for on Google or give me some pointers? I haven't chosen where my website is going to be hosted either, so if someone could tell me what to look for to know if they support running applications like this, I'd really appreciate it as well!
This could also apply to other programming languages as I know a bit of Python and C++ as well, so in the future I might have some applications in those languages I'll want to use on the web.
If I'm not approaching this the right way, I'm open to other suggestions, but the best solution would be to keep my Java program intact as I know it works exactly like I want it to and I'd rather not have to start it all over again.
If your host is *NIX based you can use crontab (Automatic Task Scheduler) to run your program at set intervals. Make it check if a "new" PDF exists, and run the program if there is. There may be a way to use Windows Task Scheduler type programs to do it on Windows. This is probably the easiest way.
Alternately you can use You can use shell_exec() in your php to execute a command on your *NIX system directly to run your java program.

Advice on which language to use

I'm trying to create a web application which will get input from system.
What this application should do is to listen what happens when some shell scripts are executing and reporting the status trough web.
An example :
I'm copying thousands of records with shell script, and while this is still executing I'd like pass the current status of the script to the web interface. From shell script side I could echo something like "The files are being copied please wait for it to finnish".
Using which programming language from shell side would be the easiest way to pass this to the web interface? I intended to use JSP/JAVA for web.
Is this even the right way to think about this? Every suggestion is welcome
EDIT
Would something like http://nodejs.org/ be useful for this?
I'd use a named pipe (FIFO) instead. You simply write your output to the pipe and let the application read it. I'm not sure if there is any other way to get a more live system than this.
I'd recommend Perl as the back-end.
EDIT:
named pipes are a special type of files on UNIX. The abbreviation FIFO stands for "First In First Out". On LINUX Journal you can find an interesting read about named pipes.
Perl is a very powerful scripting language with many ready-to-use modules which you can find on http://cpan.org. You can find some answers here on SO about how/where to start learning Perl.
The Web part of your application can easily read a file or a database, so you just need to make sure that your shell scripts are outputting something for your Java code to update.
For example, if you run your shell script like this
./myscript.sh > mylog.log
Then in your Java code (note that you should not have logic in your JSP), you can read in the file to determine the status of the update, and output the result to your JSP.
It would be better to read the data in from a database, but that would involve you changing your shell script to output the data to a database.
You could put shell script output to some storage on server. And use AJAX on the web page to poll the server and get updates from that storage to the page.
If you will decide to use JSF for web page creation I can recommend "a4j:poll" component from ajax2jsf library. It is very simple and straightforward. Very easy way to poll server from the web page.
Writing to log file would be the simplest solution. And in programming, simple often means good.
If you really need very fast/realtime system, you should probably make these logs a database.
As for language, use what you like best. They all do support SQLite, including bash.
If I understand your question correctly you want to display a web page on a client machine that reports the status of a long running task on a server machine. If that is the case then you need to focus on something called AJAX IMO.
For what it is worth the simplest and easiest to understand implementation of AJAX that I know of is Apache Wicket.

DOS batch file to enter commands in proprietary java app and receive feedback?

I'm working on a project in which I'd like to be able to turn lights on and off in the Duke Smart Home via a high frequency chirp. The lighting system is called Clipsal Square-D and the program that gives a user access to the lighting controls is called CGate. I was planning on doing some signal processing in Matlab, then create a batch file from Matlab to interact with Cgate. Cgate is a proprietary Java app that, if run from a DOS command line, opens up another window that physically looks like the DOS command prompt. I have a batch file that can check to see if Cgate is running and if not, open it.
But what I can't figure out how to do is actually run commands in the Cgate program from the batch file and likewise, take the response from Cgate. An example of such a command is "noop," which should return "200 OK."
Any help would be much appreciated! Thank you very much in advance :)
(here's my existing batch file by the way)
#ECHO off
goto checkIfOpen
:checkIfOpen
REM pv finds all open processes and puts it in result.txt
%SystemRoot%\pv\pv.exe
%SystemRoot%\pv\pv.exe > result.txt
REM if result has the word notepad in it then notepad is running
REM if not then it opens notepad
FIND "notepad.exe" result.txt
IF ERRORLEVEL 1 START %SystemRoot%\system32\Clipsal\C-Gate2\cgate.exe
goto end
:end
I don't know how to do this on Windows, but on UNIX, there is a program called expect that is designed for such a task. If you install Cygwin, you should be able to use the expect utility on Windows.
You're calling start cgate.exe, which will cause cgate.exe to be launched in a new window. First off, you probably want to run cgate in the same window, which means you should drop the start.
Secondly, you can use shell redirection to pass commands to the STDIN of cgate from a text file, like so:
cgate.exe < commands.txt
This will probably work, but it might not, depending on how cgate.exe is actually expected to receive its data.
If you want to have two-way communication, where you send in data, get the response, then send in more data depending on what the response was, you'll have to use something other than a batch file. Most scripting languages (perl, python) could be used for this purpose, or C or anything else.

Categories