Автозапуск jar файла linux

Run java jar file on a server as background process

I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can’t shut down / terminate the process. If I press ctrl + C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?

 #!/bin/sh java -jar /web/server.jar echo $! #> startupApp.pid 

4 Answers 4

#!/bin/sh nohup java -jar /web/server.jar & 

The & symbol, switches the program to run in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.

Thanks Anton,currntly im stopping the server by killing the process id.i don’t think it’s best practice.is thery command to to stop the server?

Short answer: it depends on the server. Long answer: as far as I know, there is no safe way to shut down a process without the process supporting such graceful shut downs. For example, if it’s a web server being terminated by an external signal, there is always a possibility that some requests will be lost. One way to solve this problem is to implement a graceful termination function in the server itself, e.g. by processing a special kind of requests. Then, the server can be terminated by sending it a request of that special kind. Otherwise, killing the process by its ID is the simple way.

Using nohup should always be combined with redirecting stdout and stderr explicitly — otherwise, you don’t get control of where the logs go, and end up with an ugly nohup.out created in whichever directory this script happens to be invoked from.

@Anton when I run a script(just like your answer) with SSH EXEC runjar.sh. It started streaming the logs in my local terminal and if close my terminal my jar gets killed. How can I run a jar in the background using SSH EXEC?

Читайте также:  Системный администратор сети linux

Источник

Run Jar file on Ubuntu at system startup

I’m trying to get a .jar file to run at startup on an Ubuntu machine, but I’m not getting anywhere. I’ve tried the instructions here https://askubuntu.com/questions/99232/how-to-make-a-jar-file-run-on-startup-and-when-you-log-out , and I’ve tried using info from the Upstart site & cookbook, but they haven’t worked. I’ve tried both the old SysV and the new Upstart approaches, but neither of them start the .jar on system startup. Here is the shell script which runs the .jar

#!/bin/bash cd /home/dev/TransformationService/ java -jar TransformationServer.jar 
#!/bin/bash # Transformation Server # # Description: Transforms incoming messages on a given port and forwards them case $1 in start) /bin/bash /usr/local/bin/ServerStart.sh ;; stop) /bin/bash /usr/local/bin/ServerStop.sh ;; restart) /bin/bash /usr/local/bin/ServerStop.sh /bin/bash /usr/local/bin/ServerStart.sh ;; esac exit 0 
# transformationserver - transforms incoming http messages, and redirects them # # This service intercepts incoming http messages on a given port, and # transforms them into an acceptable format in order to be received # by a 3rd party service start on runlevel [345] stop on runlevel [!2345] respawn script exec /bin/bash /home/ubuntu/TransformationServer/ServerStart.sh # Also trying the below as well #exec /bin/java -jar /home/ubuntu/TransformationServer/TransformationServer.jar end-script 

Can someone with more experience in using either of these approaches look over my files here, and potentially point me in the right direction with this? This service is needed so our companies system can successfully receive communications from one of our clients. Thanks in advance.

Источник

How to make a jar file run on startup & and when you log out?

Any ideas? Can someone point me in the right direction?

Just looking for the simplest solution.

7 Answers 7

Here’s a easy way to do that using SysVInit. Instructions:

  1. Create the start and the stop script of your application. Put it on some directory, in our example is:
    • Start Script: /usr/local/bin/myapp-start.sh
    • Stop Script: /usr/local/bin/myapp-stop.sh

Each one will provide the instructions to run/stop the app. For instance the myapp-start.sh content can be as simple as the following:

#!/bin/bash java -jar myapp.jar 

For the stop script it can be something like this:

#!/bin/bash # Grabs and kill a process from the pidlist that has the word myapp pid=`ps aux | grep myapp | awk ''` kill -9 $pid 
#!/bin/bash # MyApp # # description: bla bla case $1 in start) /bin/bash /usr/local/bin/myapp-start.sh ;; stop) /bin/bash /usr/local/bin/myapp-stop.sh ;; restart) /bin/bash /usr/local/bin/myapp-stop.sh /bin/bash /usr/local/bin/myapp-start.sh ;; esac exit 0 
update-rc.d myscript defaults 

PS: I know that Upstart is great and bla bla, but I preffer the old SysV init system.

But how do the system will know what parameter will use? How to set to be the «start». Tried here and it did not work.

Also, even after doing the 3 steps, my jar is not running on startup. Are you sure if we have to change permissions, or path or something like that?

This worked for me, thank you. But I had to make the last script (‘myscipt’ in your example) executable with this command: chmod 755 /etc/init.d/myscript . Otherwise update-rc.d command couldn’t execute it.

Yes! It is possible. 🙂 Upstart is the way to go to make sure the service stays running. It has five packages, all installed by default:

  • Upstart init daemon and initctl utility
  • upstart-logd provides the logd daemon and job definition file for logd service
  • upstart-compat-sysv provides job definition files for the rc tasks and the reboot, runlevel, shutdown, and telinit tools that provide compatibility with SysVinit
  • startup-tasks provides job definition files for system startup tasks
  • system-services provides job definition files for tty services

The learning is very enjoyable and well worth it. Upstart has a website: http://upstart.ubuntu.com/

From the Upstart website, as of 2019: «Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd»

  1. Create a Start script in /etc/rc3.d (multiuser console mode) with corresponding Kill scripts in /etc/rc.0 and /etc/rc6.d to kill your Java program in a controlled way when the system powers down (runevel 0) or reboots (runlevel 6) See An introduction to Runlevels. You might be able to start your Java app in runlevel 2 (rc2.d) but, as a crawler, it will need TCP/IP. So make sure your networking service is available/started in your runlevel 2 beforehand. Networking is definitely up in runlevel 3. /etc/init.d contains all the actual start/kill scripts. /etc/rcN.d directories just contain links to them, prefixed with S or K to start or kill them respectively, per runlevel N.
  2. A process run by crond should persist between logouts. Maybe add it to your crontab.
  3. A process run with nohup should also persist. See nohup: run a command even after you logout.

Источник

Run Your Java App as a Service on Ubuntu

Join the DZone community and get the full member experience.

Say you have a JAR file and you need to run it as a service. Additionally, you want it to start automatically if/when system restarts.

Ubuntu has a built-in mechanism to create custom services, enabling them to get started at system boot time and start/stop them as a service. In this post, I am going to share a simple and elegant way to create a service wrapper for your JAR file so you can run it as a service. Here we go.

Step 1: Create a Service

sudo vim /etc/systemd/system/my-webapp.service

Copy/paste the following into the file /etc/systemd/system/my-webapp.service :

[Unit] Description=My Webapp Java REST Service [Service] User=ubuntu # The configuration file application.properties should be here: #change this to your workspace WorkingDirectory=/home/ubuntu/workspace #path to executable. #executable is a bash script which calls jar file ExecStart=/home/ubuntu/workspace/my-webapp SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target

Step 2: Create a Bash Script to Call Your Service

Here’s the bash script that calls your JAR file: my-webapp

#!/bin/sh sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml

Don’t forget to give your script execute permission: sudo chmod u+x my-webapp

Step 3: Start the Service

sudo systemctl daemon-reload sudo systemctl enable my-webapp.service sudo systemctl start my-webapp sudo systemctl status my-webapp

Step 4: Set Up Logging

First, run: sudo journalctl —unit=my-webapp . See real-time logs by using the -f option.

If you want to trim them, use -n to view the specified number of lines of the log:

sudo journalctl -f -n 1000 -u my-webapp

Tail the live log using the -f option:

sudo journalctl -f -u my-webapp

Stop the service by using:

sudo systemctl stop my-webapp

That’s it! Enjoy and show your support if you like it. Thanks!

Published at DZone with permission of Muhammad Sarwar . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Оцените статью
Adblock
detector