Raspberry Pi - Executing external commands with an app as a service - java

I use the script of PbxMan to run a Java app as a service on this answer.
All work perfectly when I launch my app in terminal but when it's running as a service, external commands are not executed.
Example of how I execute commands :
String [] startStreamWCTour = {
"/usr/local/bin/mjpg_streamer",
"-i", "/usr/local/lib/input_uvc.so -f 10r VGA",
"-o", "/usr/local/lib/output_http.so -w /var/www/cam"};
Process p = Runtime.getRuntime().exec( startStreamWCTour );
...
and nothing is executed...
Maybe I have to add something to my commands...? ( I'm a little newbish on Linux ;) )

Related

Restart Tomcat with Java

I need to restart the tomcat from Java code.
For eg, if a query is not executed for a certain time Period then it will restart the tomcat automatically.
I have tried the following shutdown and startup code, but when we shutdown the tomcat then the java code will not run and tomcat not started.
Note :- I am running this code from a application and restarting the same tomact which the same application is using.
Following the code
try {
PreparedStatement.setQueryTimeout(10);
rs = PreparedStatement.executeQuery();
} catch (SQLException ex) {
System.out.println("IN CATCH BLOCK FOR THE REFRESH INVOICE");
String shutcommand = "killall java";
Process shutchild = Runtime.getRuntime().exec(shutcommand);
System.out.println("JAVA PROCESS KILLED");
String locationCommand = "cd /root/cluster/tomcat6/bin";
Process locationChild = Runtime.getRuntime().exec(locationCommand);
String strtcommand = "./startup.sh";
Process strtchild = Runtime.getRuntime().exec(strtcommand);
}
Killing tomcat after SQLException this is not good idea to handle this exception. Probably the problem is on database site.
But if you are sure, that it is what you need you can kill this java proces in this section, but to run tomcat you should use for example bash and cron. Why? Beacues after killing your executing code will stop, so you don't achieve the line to start tomcat.
How to check tomcat: Is Tomcat running?
TL;DR
File binaryDir = new File(System.getProperty("catalina.home") + File.separator + "bin");
String restartCommand = "\"shutdown.bat & ping 0.0.0.0 -n 4 & C:\\WINDOWS\\system32\\net start Tomcat8\"";
new ProcessBuilder("cmd", "/c", restartCommand).directory(binaryDir).start();
Survive
Creating new process will survive JVM shutdown. If you combine commands in one line
it should work fine according to my tests and this.
shutdown.bat
You need to use shutdown.bat instead stopping windows service because it often fails on Windows with message Cannot stop service Apache Tomcat...
ping 0.0.0.0 -n 4
You need to wait some time after shutdown otherwise you will get Service is already starting. Try again later error message. Also note I use ping instead timeout because it causes problems on some systems.
C:\WINDOWS\system32\net start Tomcat8
I'm starting windows service because invoking startup.bat won't work for me. Also remember to replace Tomcat8 if you using different tomcat or custom service name for example Tomcat7
Redirect I/O
Don't redirect input or output of process instance or command will shutdown with JVM, and Tomcat won't start.
You can execute this native command using java
String command = "c:\program files\tomcat\bin\startup.bat";//for linux use .sh
Process child = Runtime.getRuntime().exec(command);
You should consider using ProcessBuilder instead of Runtime exec. Also, you should split all the arguments when you want to execute a command.
I suggest this :
ProcessBuilder shutcommand = new ProcessBuilder("killall", "java");
Process shutchild = shutcommand.start();
System.out.println("JAVA PROCESS KILLED");
ProcessBuilder strtcommand = new ProcessBuilder("/root/cluster/tomcat6/bin/startup.sh", "java");
Process strtchild = strtcommand.start();

Execute bat-file from windows service without losing arguments

I have to execute a bat-file within a C# windows service. The bat-file executes a jar-file with some arguments. That works without any problem.
But i noticed a difference between executing the bat-file:
1. via the windows service:
I am able to look up the start-arguments, when i query Win32_Process.
2. not via the windows service (e.g. double click on bat-file):
The start-arguments are not listed in the queries Win32_Process. But the jar-file within the bat was started correctly with the arguments...
How can i execute a bat-file from a c# windows service, so that i can query the details later on?
My current code:
Process p = new Process();
p.StartInfo.FileName = "BAT-PATH";
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.Start();
Content of the bat-file:
java -Dspring.profiles.active=postgresql ^
-Xmx1G -Xms1G ^
-jar C:\PATH\TEST.jar

how to restart tomcat from a running webapp?

I need to restart tomcat service from a webapp running on this tomcat. So I'm trying to execute script that stops tomcat service, and then starts it:
echo "before stop" >> textfile.txt
NET STOP "Tomcat7"
:loop
timeout 3
SC query Tomcat7 | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
goto start
) ELSE (
goto loop
)
:start
NET START "Tomcat7"
Java code:
String command = "C:\\Tomcat 7.0\\bin\\restart.bat";
Process p = Runtime.getRuntime().exec(command);
Tomcat is stopped, but not started.
If I run this batch from command line, it works properly.
thank you for your time
This worked:
String fileName = "C:\\Tomcat 7.0\\bin\\restart.bat";
String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
Runtime.getRuntime().exec(commands);
taken from http://www.rgagnon.com/javadetails/java-0014.html
What you are asking is not exactly safe and possible but do take a look at Tomcat manager API that allows you to programmatically manipulate Tomcat deployment and instance:
http://tomcat.apache.org/tomcat-7.0-doc/api/index.html
http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/manager/host/HostManagerServlet.html
Agree with Edmon.
Tomcat is a provider of containers. Each container should act independently of each other, even if they call the services another provides. This should all be done via RMI or alike.
Like Edmon also suggests, you could call using the API, but again... sounds bad. Instead, question why it needs to restart. Then, if there's no work around, use the Tomcat Manager.

Write a java program to monitor an application running in linux remotely

Dear all,
I want a java program which can connect to a remote host that runs on linux os and able to start and stop any application from that remote host. Is it possible to do this task with a java program, with or without any service wrapper!
Check Ant SSHEXEC task.
link...
Looks like there's no AntBuilder implementation in Java. Groovy is the ideal tool for these kind of tasks. You can write a Groovy and access it just like any other class in Java. If you are using NetBeans, then Groovy and Java can co-exist in the same package.
def String execute(def cmd, def host, def uname, def pwd)throws Exception {
def ant = new AntBuilder()
ant.sshexec(host : host,
username : uname,
password : pwd,
command : "ls -l,
trust : "true",
outputproperty : "result")
return ant.project.properties."result"
}

Invoking a Remote .bat file over http

I have a .bat file on a remote machine. I want to invoke it through http call. I dont want to make any changes on the remote machine. Is there a way to do it using java and http?
String command = "cmd /C start C:/Users/abc/Desktop/test.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
The above works good to invoke a .bat file on local machine. I would not mind considering other ways too, but invoking it through http would be the first choice.
EDIT:
I am using paramiko to do this now. However,I am unable to run the remote commands on the command prompt.
ssh = paramiko.SSHClient()
print "Enter the IP address"
ip = raw_input("ip>")
print "Enter the username"
user = raw_input("username>")
print "Enter the password"
pwd = raw_input("password>")
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=user, password=pwd, allow_agent = False)
print "connection successfull"
i, o, e = ssh.exec_command("echo test") # example command
s = e.read()
if s: # an error occurred
raise RuntimeError, s
result = o.read()
print result
Somehow it says AllowDesktopAccess failed
You need a service on the remote machine, for example an http server that is configured to run this script on demand (eg via cgi) or an ssh server you can connect to to issue the command.
Since you're using windows (I assume) then PsExec may be the service you need.
http://technet.microsoft.com/en-us/sysinternals/bb897553

Categories