Executable jar run permanently [duplicate] - java

This question already has answers here:
Run java jar file on a server as background process
(4 answers)
Closed 2 years ago.
I am running an executable jar using the command prompt. But after command prompt closing the execution is stopped. I need to run this permanently. Because this is a microservice.
How can I achieve this goal
I am running using java -jar JARPATH

There are several options:
1) Creating a service as was mentioned in the first answer and comment.
2) Run a process in the background.
nohup java -jar my-service.jar &
Where nohup command enables a process to continue running in the background when a user exits a shell.
To terminate the service, you need to kill the process.
You can create a run-script writing PID to a variable (or a text file):
#!/bin/bash
my-service &
export SERVICE_PID=$!
And kill the PID in stop-script:
#!/bin/bash
kill -9 $SERVICE_PID
3) Run process in a container, e.g. Docker
Running process in container allows has some advantages and allows to manage many different options rather than just 'run-and-stop'.

Simply create a service to invoke jar file. Services can run indefinitely and can be triggered at startup or can be started or stopped manually.

Related

Centos java custom service

I have a script.sh that set some environment variable and start a java server.
#!/bin/bash
export JAVA_HOME="/opt/java"
export ....
nohup $JAVA_HOME/bin/java "$MEMORY_JAVA_OPS" -classpath "$MY_CLASSPATH" $MAIN_CLASS &
I would like to transform this script (now is launched by /etc/rc.d/rc.local) in a service.
I tried many examples found online and over StackOverflow.
I created myservice.service file using many templates found online... No one work!
one example is:
[Unit]
Description=MyService Java Process Restart Upstart Script
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
User=root
TimeoutStartSec=0
Type=simple
KillMode=process
#export JAVA_HOME=/opt/java/jdk-9
#export PATH=$PATH:$JAVA_HOME/bin
WorkingDirectory=/tmp/myworkdir
ExecStart=/path/to/myscript.sh
[Install]
WantedBy=multi-user.target
With some configurations, the service starts but the status command says that it is dead (while it is actually running). With others it does not start. With none it stops with the command stop ....
I tried Type=Simple, forking, oneshot... always some problem.
I would simply that after boot or when user launch systemctl start myservice, service start, and if after some time crash will be started again. And if I will run systemclt stop myservice it stops and not need to kill the process.
Firstly it need to be said, that concept "service" greatly differs in Linux/Unix and Windows environment. From your question seems to me you are looking for Unix solution.
In unix you typically register some statup and stop script/command. The startup script just runs your java application via java -jar app.jar. This application does business logic & also opens listening on some SHUTDOWN port.
The stop script/command just invokes another (or the same with different cmd parameters) java application which does nothing else just sending STOP command to original application's SHUTDOWN port.
You can look in more detail for example on tomcat startup/stop scripts - they are doing exactly this.
For windows is better to use some wrappers like WinRun4J or whatever else. Of course you can have one multiplatform maven archetype for "universal multiplatform" service like we do.
EDITED:
If you are still unsure how to configure it on Linux, read https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux
ExecStart will be the startup java -jar app.jar and ExecStop will be the stopping command java -jar app-stopper.jar

Prunsrv exe killed via task manager but child process remains

I have a Java application run as a Windows service using procrun (specifically prunsrv). The service is defined as an exe StartMode so a batch file (run-my-app.bat) is run as the StartImage. Why I am not using jvm or java mode is a different story, not related to this issue (I was unable to run spring boot application with procrun, all examples did not work so I resorted to creating a batch file and calling java -jar my.jar). prunsrv.exe is actually renamed according to the application, say myapp.exe. The problem is that if myapp.exe is killed via the task manager, the java process remains! The batch file run-my-app.bat runs the application using the following line:
start "%APP_NAME%" /b "%JAVA_EXE%" -jar myapp.jar --spring.config.location=application.properties --logging.config=log4j2.xml
The batch file completes and the started Java process remains - I know this because if I print a message after the above "start" command I see the message in the log.
Is there any way to stop the java process when the prunsrv.exe (renamed myapp.exe) is killed?
Child processes will only be closed if they were created as Job objects.
IMHO it's not possible to tell prunsrv.exe to start processes as jobs, so the answer to your question is No.
You can of course terminate every single process individually. There are attempts to kill process trees, but be aware that Windows does not maintain parent-child-relationships. That means: If in a chain of 3 processes the middle one dies, the tree is not available any more.

Spawning process in background on Jenkins - job that won't stay in queue

I want to make job on Jenkins that starts server (MockServer on WireMock).
Server is launched from *.jar file, from terminal like that.
java -jar serverLaunch.jar
It takes over my console. To avoid that I modify this and do:
java -jar serverLaunch.jar &>/dev/null &
And that works for me on my local PC. Now I want to move it to Jenkins.
If I try to do this from "Shell command" block in Jenkins Job then:
a) java -jar serverLaunch.jar
I have task locked in queue in my Jenkins and I don't want that but server starts and works.
b) java -jar serverLaunch.jar &>/dev/null &
Job ends with success but my server is not alive.
I have wrapped this command also in .sh script and .rb script. Any idea how to make it work?
I've tried this:
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
And then in Jenkins "Shell script":
daemonize -E BUILD_ID=dontKillMe /bin/bash launch.sh
But it also passes but server is not alive.
I had to check "Inject environment variables to the build process" and add:
BUILD_ID=dontKillMe
Now it is working.
Try using nohup e.g.:
nohup java -jar serverLaunch.jar &
That should prevent the process being terminated when the parent shell process exits (which I suspect is your problem).
Another effective approach would be to add a post-build action that executes a shell spawning the server.

How to restart service when it get stopped in Linux [duplicate]

This question already has answers here:
How to Daemonize a Java Program?
(11 answers)
Closed 7 years ago.
I have built a little daemon in Java and I would like to run it as a service under Unix (e.g. Debian 5). I have read that there is a possibility of using a Java wrapper, but isn't there any other option which is easier to implement? Can't I just use a Unix command such as xxx java -jar program.jar?
Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:
$nohup java -jar program.jar &
You need to create an appropriate script in /etc/init.d and link it to /etc/rcX.d directories. The script should support at least start, stop, and status parameters. During start it should run java command with appropriate arguments, probably via nohup java <arguments> &. Then you should save PID of your newly-started process to file /var/run/yourservice.pid. stop command should read this PID file and kill this service.
The details vary from distribution to distribution, most distributions provide some macros to make whole job easier. It's best to look at examples of other services in /etc/init.d for your distribution.
Additionally:
If your service isn't accessed from other computers from the network, but it opens some port, make it unavailable with firewall.
If your service processes some 'delicate' data, it's good to add another user and invoke an appropriate sudo command in your /etc/init.d file.
You can start it as:
java -jar program.jar
Unix daemons are normally started by init or started by a script in /etc/init.d or /etc/rc.d, and started at specific runlevels - normally by soft links in /etc/rcX.d. (where X is the intended "runlevel" which is normally 3.
I think debian are moving to using "upstart", a init-replacement. It uses config files in /etc/init to define jobs, and they are quite easy to write. Check that out.
Daemons traditionally closes stdin, sdtout and stderr, and does a "double fork" when starting, in order to detach from the session and also to signal that they are ready to handle whatever they should handle. This is not really necessary, as long as the daemon is not started from the terminal.
If you want a simple shell wrapper to start you program; you just need to write a small shell script:
#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar
... and make it executable (chmod 755 )
This article contains a few useful tricks for running a Java application as a daemon:
http://barelyenough.org/blog/2005/03/java-daemon/
Alternatively, you can have a look at the Apache Commons Daemon project, although this requires native code (Unix and Win32 supported):
http://commons.apache.org/daemon/
You can use a cron job to schedule your program. You can also check out this article for details on how to run scripts on startup. You can write a script that runs your java program and run it on startup as mentioned in the article.

How to set up a cron job to start and stop a java jar?

I have compiled my JAVA code into a jar file which I have ported to my ubuntu server. I can start it manually the usual way using java -jar myJar.jar but I'd like my program to be active only for 8 hours. How can I go about setting my jar file up as a process which starts at 9AM and also which automatically closes at 5PM?
I would write a simple launcher script that does the following:
Takes two command line options:
--start
Set up the classpath and environment like JAVA_HOME for the jar to run.
Spawn java -jar myJar.jar.
Capture the process ID and store it in the myJar.pid file in a specific location.
--stop
Read the process ID from myJar.pid and send a kill signal.
Then schedule two jobs in cron, one to call this launcher script with --start argument, at 9AM, and the other to call the same script with --stop argument, at 5PM.
I would also have a shutdown hook registered in my application to gracefully exit when the kill signal is issued.

Categories