How to faster the execution of shell_exec? - java

So I have a shell exec command that run a a java command, load a jar file it ask for input then produce an output in the browser. I'm done with the coding and its working just fine, but the problem is its a bit slow. So, is there a way to make it faster? A certain command or something?

Related

run Java commands through a Shell script

I'm trying to code a shell script to start/stop torrents using vuze's console UI through SSH:
https://wiki.vuze.com/w/Console_UI
I've downloaded vuze and everything works fine until I type this command:
java -jar Azureus2.jar --ui=console
After that, no command in my script works unless I quit that console.
Any solutions please? And if it's not feasible using shell scripts, any suggestions please?
Thanks.
Basically, the moment you run that command, your java program runs 'in the foreground', which means the rest of your script stops executing until your program exits.
If you want to keep on running the rest of your script while your java program executes you have to run your program in the background. One way to do that is as #Alp suggests:
java -jar Azureus2.jar --ui=console &

force tty creation for crontab job

I've been coding a litte bash script which connects on several distant servers, then execute a java CLI program through a few expect instructions.
It goes like this :
bash script
expect
ssh to server using public keys
expect ...
expect ...
log_file my_file (everything displayed on the screen is now redirected to my_file)
expect ...
log_file (closing my_file)
exit
exit
When I execute my script manually everything runs OK.
When I execute it through crontab, the file my_file is empty.
I found out that cron jobs don't have a tty attached and that PATH isn't the same as usually
My question is : is there a way to force the creation/allocation of a tty to my cronjob?
I've tried using the -t and -tt option with ssh but no result.
redirecting standard output on different levels of the script didn't work.
Also, I can't install screen (which could have helped, maybe) and "script" isn't writing anything either.
Thanks a bunch!
You can check the cron tab log for erros and make sure the full path is given for the command to be executed.

How to Run sh file from java in linux as background process

I need to run Sh file from java class.In that java main class is from main class it calls another sh file it takes data from db writes to csv file.It should run continuously.in back ground how to make this run.I made thread as setDaemon.But I am not sure that is it running Back end or not?? When i am running manually that sh file is creating but when i am running backend i did't get any log file or logs.
The "&" works in this case like "|" etc.:
Runtime.getRuntime().exec("./test.sh &");
(I made a test on Debian with and endless loop in a shell script and the java got back the control after starting the script)
Good article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Its for the old way but gives good insight.
Article I haven't read of converting to the new way:
From Runtime.exec() to ProcessBuilder

Python run Java command line arguments with incrementing variables asynchronously.

I have a java program which does individual jobs e.g. takes in a file, does some processing on it and creates a new file. To run it I have to type the following in the command line.
java myprogram.jar -input myfile1.txt -output output/myfile1.txt
However i wish to batch process a few thousand files, so i would like to increment the number at the end of the myfile part of the string. So once the first job is finished, the second job will then start, and so on so forth. Rather than have thousands of instances of the java program running at the same time.
Any help would be appreciated.
Jon
I would use bash or something, but if you need to use python, you can use subprocess.call to do this:
from subprocess import call
for i in range(1,1000):
call(["java myprogram -input myfile%d.txt -output" % (i)])
This is a perfect use for a bash script (if you're in a *nix environment) or a .bat file if you are in Windows. Bash example:
#!/bin/bash
for i in {1..5}
do
java myprogram.jar -input myfile$i.txt -output output/myfile$i.txt
done
I would suggest just modifying your Java program to handle processing a whole directory so instead of handing over files pass over a directory to work on then the java program would process all of the files in the directory and write out several output files. Use some simple name mapping scheme for the output. That way you could exploit threads to handle several files at once should you want to boost speed for multi-core boxes. Also that keeps your overhead low because only 1 JVM is running.
You don't have to modify your Java program to do this. You could write a new program that leverages the code out of the JVM.

Powershell process hanging when called from Java application

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process

Categories