Why my cmd program always be paused automatically? - java

When I use cmd(start->run->cmd) to run java ant build or something else program. Normally The command will run the program continuously until all tasks has to be done. on the process of runing, the cmd consle will output specific some log continuously.
But My promblem is that the cmd always be paused automatically after few minutes. it will be restart if I phsyically enter any keystroke to awaken it up. it's really boring I need keep watch on the cmd.exe on all time.
Any way or configration to solve this, or any tools instead of windows cmd.exe to archieve the same function?

I can think of a couple of possible explanations:
Your Java application is pausing when it tries to read something from System.in.
You are actually running a batch file that uses the "pause" command at some point.
(FWIW: the cmd.exe program also implements pausing / continuing using CTRL-S / CTRL-Q, but (AFAIK) that requires you to physically enter the control characters from the keyboard ... and that's not happening.)

Related

Is there a way to pause/resume execution of a running Java program in Eclipse on Windows?

In Linux, it's possible to pause/resume execution using kill.
Is there a way to do that on Windows?
You can go to Eclipse console and click red square for the process you want to stop. The different processes running are available in dropdown on console itself.
Many a times, processes like SVN synch etc. just hang up and user has to close Eclipse itself and restart it. This is something observed in Eclipse version 2020 as well.
On linux, kill command doesnt pause the process, instead it kills its pid and stops the process.
To do the same on Windows:
You must use Task Manager (Alt+Ctrl+Del on Windows) to kill process. Go to tab Processes, find process javaw.exe* and click End Process. javaw.exe is the java process without any console.
So, process in Eclipse will be closed but your Eclipse will not be closed.
Kill on Linux actually stops the process, you cannot resume it.
If it's a console application and you just want to pause at a certain known execution point in your code, just use a Scanner and wait for a user's input
Scanner scanner = new Scanner(System.in);
scanner.nextLine();

java not waiting for unix binary to complete execution

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.

How do I stop a daemon thread that does scheduled database backup ?

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 continously run my java threads

I am running my java application where i have used threads...
i am running this application using ant command on the terminal..
But when i close my terminal or press ctrl+c,then java program which was running stops...
Please help me out to solve this as i want to run this program continously...
If you kill the Java process, Java will no longer be running. If you want the threads to keep running continuously, the Java program must remain active.
Invoking such a program with ant is not usually the way to do it. On Unix-like systems, you would typically run such a program in the background via /etc/init.d startup scripts. In Windows the equivalent would be starting your program as a service, though I'm not sure of the intricacies involved in getting Java to run this way.
If you're running something from a concole - how about just not killing it and minimising the console? If you're starting it from Linux (or Cygwin) just append a & to the end of the command line and the process will run in the background.
Tell us more about your environment, and what compromises you're prepared to put up with (e.g. having a minimised console window sit in the taskbar) and we can help you more. At the moment, the only definitive answer I can give is that "yes - Ctrl-C will kill your program (as intended). If you want it to keep running, don't tell it to stop running." :-)
You can run your application as a service in linux or windows.
Have a look at the screen command for Linux.
I guess this is desired behavior. If you terminate your application, It gets shut down.
If you wish to run the application in the background you should consider making it a windows service or a deamon.
If you wish to continue running it as a application on *nix, you can consider to use GNU Screen.
Run the ant command in the background and redirect its output to a file:
ant &> my.log &.
NB: this issue does not seems thread related (unless I've misunderstood it).
It sounds like you want to daemonize your ant task. I'd suggest the following command:
nohup ant &> ant.log < /dev/null &
The nohup will allow the program to continue to run after you close the terminal.
I'm surprised nobody has mentioned the all-too-famous Java Service Wrapper. This is an excellent piece of software if you are developing long-running processes (a.k.a services).

Can a Java.exe JVM restart itself somehow?

Can a Java.exe JVM restart itself somehow? If you have a Java program running in a DOS shell (NOT as a service), what is the cleanest, most efficient way for it to shut down, close the shell, and restart itself with a different Windows process id and re-open in a different shell with a reloaded classpath?
I expect it's not possible but I thought I should ask and learn from other people by asking.
NOTE: If anyone knows how Eclipse IDE is able to restart itself so cleanly, that is the method I am wanting to use.
The Java Service Wrapper can help here. You can run your Java process under it but not necessarily as a service. You can define restart characteristics, such that if it exits, the Java program is restarted (either by System.exit() or calling WrapperManager.restart()).
The cleanest and simplest way to do that would be to run the program from a DOS batch file (or CMD script, if you prefer). Build a loop into your DOS batch file. You put a :label near the beginning and a GOTO label below your Java process and off it goes. If need be, you can break out of the loop using Ctrl-C.
Create a separate executable that takes a process ID and a command as command line argument, waits for the specified process to close and then runs the specified command. Launch that program, then exit the current instance.
You can either embed that program in your primary program and copy it to a temporary directory to execute it, or include it side-by-side with your program.

Categories