I'm running Hudson as a windows service through Tomcat, with no slaves involved. The last build step in the job is a batch file that invokes some Java code. The code uses PostgreSQL's command line tool psql (via Runtime.exec()) to create a database on the local machine and eventually run some tests against it.
The job will progress to this point, then hang indefinitely without starting to create the database. If I run the batch file from the command line, it works perfectly. I don't think http://hudson.gotdns.com/wiki/display/HUDSON/Spawning+processes+from+build applies, since the process spawned doesn't even seem to begin executing, but I'm new to this so please let me know if I'm wrong.
Edit #anjanb:
The batch file's only purpose is to invoke the Java code, and the only user input is being passed in as command line arguments, which I can see are going in directly via the build's console output.
Process Explorer is showing that psql is being started, but it's obviously not being executed, since the first command psql is given is to create a new database, but that's not happening.
Edit 2: I've gotten some tips from the Hudson users mailing list, I'll try them out on Monday and report back.
Edit 3: The Java code was already consuming the output streams, I used that article when developing the code. I can't figure out what's going on, so I'm redeveloping the code to use JDBC to create the database, instead of relying on psql and Runtime.exec()
Do you read the output of the process ? If it produces more output than the OS buffers can handle, you need to read it...
Also, some processes wait until input has completed. Try to call process.getInputStream().close() after starting the process.
Maybe this article is also interesting. It's called "When Runtime.exec() won't":
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=2
There is a possibility that the program is waiting on some user input. If the service is not configured to accept user input, it will appear to be hanging.
YOu can try by configuring the service to allow USER INPUT(GUI) -- that might help.
Also, you could run Sysinternals ProcessExplorer and ProcessMonitor -- they will be able to find out where the .BAT job has stopped.
Related
I have a java program that should run on a Windows machine. It should run "forever", i.e. when the JVM or the program crashes, it should be restarted. When the computer is restarted it should also be restarted.
I saw advice to wrap the program as a "Windows service", but the tools I found seem to be either costly, complicated or outdated.
Can somebody describe me a straightforward way to achieve the desired behaviour?
For the part where you want to start the program after restart you can create a simple batch (.Bat) file and u can put that file in the startup folder.
Also you can use the same file for running the program when it crashes. you can use tasklist command and check if your java program is running and if it is not .just start the program.
Just check our windows batch this is one of the best things you can get everything for doing anything on windows without anything expensive
Yet Another Java Service Wrapper is a tool that easily wraps your Java program into a Windows service. Just start the program, note down the PID and enter it into the wrapper. Two things, which are probably universal to services, should be noted:
For connection to the network, you need to specify an account with the necessary rights.
Connected network drives are not available.
Im creating a javafx application to run a unix binary application, which limits the percent of cpu a process can use, herein i already have a cputhrottle unix binary which receives PID and CPU percentage as argument, my application just acts as a GUI for this unix binary.
When I run this cputhrottle from terminal it keeps on running, until i press - CTRL+C (this is the normal behavior).
But the problem is, when I run this unix binary with my java code, it seems, as if the program is almost immediately terminated (whereas it should be running, until I press the stop button), and because its not running & being terminated, My program cannot control the percentage of CPU a process gets.
Here's my code : http://pastebin.com/rbG3ctfH
so, is there a way to tell java to keep running the cputhrottle binary file, instead of closing it.
I've tried replacing the 56th line, and instead wrote code to execute a script, which would create a file, with some text content, & when executed he program, it did created a file, so its working, but when i add e1.printStackTrace in the catch block, it stops working for some reason.
And also, if I add sleep in the shell script, then its not executed Properly, more over no Exception is being Generated.
The command you are trying to run ("echo dkkdk|sudo....) is a shell command. You need to tell runtime.exec() to run 'bash -c echo dkkdk|sudo..'
Also be aware that runtime.exec is asynchronous and could/might/likely will return to your program before the child process completes.
Also, I could not pipe the superuser password to sudo like you are trying to do. Not saying it doesn't work for you just that I couldn't get my system to take it.
I've written a very simple command line programme that uses mysqldump to dump data at a specified interval. I wanted it to be running after I run the programme and I disconnect putty ssh connection.
But as soon as I exit the ssh connection, the programme shuts down.
I think I can make my programme run even after I disconnected from the ssh by using daemon threads but I'm worried about not being able to find and stop unnecessary duplicate daemon threads.
The scenario I'm decribing is...
1. I log into the server using putty.
2. Turn on the auto backup programme.
3. I exit the putty connection.
---the daemon is running
4. Hopefully, when I log into the server again, I have a way to stop the auto backup programme if needed.
Cron jobs and DBMS specific methods are out of my options. I'd like to learn how to do the job described above and use it where ever the situation fits not just database backup.
Any good ideas ? : )
In short, you have to launch your program in a special way, and you have (at least) two options: nohup or screen.
Let's now discuss why and how each one works.
nohup
Java's daemon threads is not what you are looking for, they have nothing to do with the issue. You can use normal threads (or even a single-threaded java program). You just need to change the way you launch your java program.
I have many executable jars that run as "daemons" on a bunch of servers, and I made a simple launch script that prepares the environment and makes it possible to terminate the SSH connection without stopping it. The main part is how to invoke the JVM: you use nohup.
nohup java -jar myfile.jar > stdout.log &
From nohup's man,
nohup - run a command immune to hangups, with output to a non-tty
So, when you terminate your SSH connection, it will send SIGHUP to all processes it started which would terminate them as you are observing. With nohup, however, your process is immune to it.
Also, note that I redirect the standard output to a file called stdout.log. This is done so that you can see whatever your program writes to STDOUT (generally some logging information that would be useful for debugging).
To terminate your program, you can use jps to list the PID of your process (say it's 123), then call kill 123. Note that for this to work your program needs to correctly handle this kind of shutdown (which involves adding a shutdown hook with Runtime.getRuntime().addShutdownHook(...) which will terminate all the threads you launched).
If, for whatever reason (a bug, or you didn't implement a graceful shutdown), the program won't terminate after issuing the kill command (which sends the process a SIGTERM), you can change the signal it sends to SIGKILL with kill -9 123, which will simply destroy the process. Mind that this can be as dangerous as a power failure (ie, suppose you are in the middle of the try block of a try {} finally {} -- your finally block will not execute!).
screen
There's an alternative, which is to use SCREEN. With it, you launch a shell that is also immune to shutdowns, and that you can share among many connections. To use it, connect to your server, and then:
screen -R
A new shell will start, in which you run your java program as normal:
java -jar myfile.jar
To make it go to the background, just press ctrl+a ctrl+d. To bring it back to the front, just execute screen -R again. If you wish to terminate your program, you could do so by entering the screen session again and pressing ctrl+c (if your java program correctly deals with this kind of shutdown).
Two possibilities for scheduled tasks, in order from least to most complex, are the TimerTask and the Quartz Scheduler. Both offer the option to cancel/delete the scheduled job.
I want to run a java program on a remote machine, and intercept its logs-- also I want to be able to know if the program has completed execution, and also whether it was successful execution or if execution was halted due to an error.
Is there any ready-made java library available for this purpose? Also, I would like to be able to use this program for obtaining logs/execution completion for remote programs in different languages-- like Java/Ruby/Python etc--
If you're only looking to determine when it has completed (and not looking to really capture all the output, as in your other question) you can simply check for the existence of the process id and, when you fail to find the process id, phone home. You really don't need the logs for that.
You should take a look at the Apache Commons Tailer for reading from another process's logs. As for checking if a process completed successfully, that's a little bit trickier. I would wrap the execution of the process in a shell script that writes out the status to a file that the Java program could then check. See here for more info on how to do that.
I want to run an AHK executable and then have java wait for that executable to terminate it's own process at the end of it's script and then continue, is there a function to monitor whether or not a particular exe is running? And if it's running to wait until it has stopped?
for example in AHK you could do:
IfExist, Appname,
{
insert code
}
I would do this project using just AHK but because AHK is so flimsy with variables and incrementing i can't get it to work it's IF and Else Statements properly
or if even possible, how can i make Java pause itself for a period of time
all im really looking for is a simple bit of code to run a .exe and the a bit fo code to wait for it to not exist
You want to read up on java.lang.ProcessBuilder to launch the exe file and then java.lang.Process.waitFor() to wait until the process is complete. If you want to feed data to the process via its stdin, or read its stdout or stderr, then you have a much more complex task involving threads to handle the streams so that the process doesn't block.
It is as simple as one line of code:
Runtime.getRuntime().exec(<insert executable file here>).waitfor();
PS: It is just one line so I didn't mind, but this is not a place to obtain full working code from others. The usual response is "look here, do it yourself", unless the OP post relevant code with a small part missing, which is not the case here.
PS2: Please consider accepting Jim's answer as it was the correct one.