How to track a program/process is running in Ubuntu? - java

I need to find a way to track that a program P is running. My program is in an Ubuntu docker container, it is a java one. I need another program to periodically tell me that my program P is running. If it fails to send me that message I will have to re-start the program. Is there a way to find out this? We are afraid that the program or the docker container could stop running and affect the user experience.

Though, the question is to broad to be answered clearly I'll try to offer some solutions.
First of all, if your java process 'fails' the container will end it's work. Based on this you can identify if your app is running simply by performing docker ps.
If you want some handwritten solution you can implement a health-check rest endpoint in you application and hit it periodically to ensure the app is running.

you could setup a restart policy on the docker container, that way if java encounters an exception and exits, it will automatically try to restart the container.
In the docker run command, just add --restart unless-stopped, this way it will always try to restart unless a stop command was send by docker itself.
more information on this functionality can be found at:
https://docs.docker.com/config/containers/start-containers-automatically/#restart-policy-details

Related

Docker entrypoint.sh for self-updating programs

I want to run a (java) application in a docker container. This application has an update functionality. This is run automatically on startup and can be triggered manually from the (web) GUI. The update downloads the new files, then runs the new executable (with new PID) and exits itself.
Usually, I use the exec java -jar MyProgram.jar "$#" in entrypoint.sh. This will stop the container when an update occurs (because PID=1 exits).
Just using the line without exec does not correctly forward SIGTERM/SIGKILL for docker stop. While forwarding these signals is possible as explaned here in program.sh, I don't know how to track PIDs when an update occurred and a new process is spawned.
Update: Since I am not the developer of the application, I am looking for a solution without modifying the applications.
Update 2: I found out that the application creates a MyProgram.pid file which I can read to kill it with the help of a trap as explained in program.sh linked above.
Typically we bake jars into an image. If we update the jars in a container at runtime, when we stop the container we lose those changes. Normal upgrade practice is to update your app spec with the new jars and rebuild it into an image using a pipeline of some sort, then replace your containers with the new ones.

java process finishes after some time with no particular reason

I have a java .jar file that i launch on an AWS instance in detached mode. So when i exit the ssh session, it still runs.
The app does some network stuff, and is expected to run for days until it finishes it task.
I have made logs all over the app, made log in the end of main method. I also made a global try/catch and added logging to the catch section.
Still, after some days i enter into ssh and see that the app just stopped running. No exceptions, main method did not complete because the log in the end did not trigger. It seems that the process was just killed in the middle of working. Sometimes it works for 5 hours, sometimes for 3-4 days without stopping.
I have no idea what could be the cause of this. I expect the java process to run until it finished, or until it crashes. Am i missing something?
upd:
it is an aws t2.micro, i think, the free tier one. It runs ubuntu 18.04.3 LTS
You need to monitor the server and application. The first thing to look at is your instance cloudwatch statistics for any CPU or memory spikes. If you find one, you know what you need to fix if you want to run your application on micro instance. For further reading
Monitoring Your Instances Using CloudWatch
Alternatively, you can collect and dump the java process statistics regularly when you are running the application. This can give insight of how heap,stack and cpu usage. Check this SO post for further details :
How do I monitor the computer's CPU, memory, and disk usage in Java?

Let java program run indefinitely - restart when it crashes

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.

OOM using CRON but not using SHELL

When I start a java program with high memory usage ("-Xmx52g") by shell, everything is working well. However if I start the same program with the same command and same user by CRON, I get a java.lang.OutOfMemoryError just after a few seconds.
Additionally CRON isn't able to do anything as long as I don't kill the blocked java program. No matter which cronjob should be started, it always ends up with "(CRON) error (can't fork)" in syslog. After killing the java program all new cronjobs are working fine again.
The problem only occurs with Ubuntu 16.04, all older versions worked very well. Is this a bug or a new security feature? I didn't find any information about this issue, so I hope anyone may help.
You need more RAM or size to launch it
http://javarevisited.blogspot.com/2011/09/javalangoutofmemoryerror-permgen-space.html

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).

Categories