Linux Launch Java Program on Startup (Ec2 Instance)

Linux Launch java program on startup (EC2 instance)

It's possible you can create a script java_server_launch.sh like this:

 #! /usr/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/your_username/DocumentManager/DocumentServer-0.2.jar
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"

Put your script under /etc/init.d directory, and then use the command:

update-rc.d java_server_launch.sh defaults

more on update-rc.d command by using man update-rc.d.

Hope this help.

Regards.

AWS EC2 Amazon Linux 2 How To Run Java on Boot

Turns out I was misreading what to paste into the user data

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
cd /home/ec2-user/
java -Xmx1900M -Xms1900M -jar server.jar nogui
--//

How to automatically start 2 Java jars on AWS EC2?

I am not a unix expert, but I see the only issue in running 2 java commands from terminal is that unless the first command returns, the next command is not executed. So, I think the solution would be run the 1st command in some interactive mode so that the other commands can be executed simultaneously.

There are ways in unix shell to run a command in background. I found this useful link - https://www.maketecheasier.com/run-bash-commands-background-linux/

In bash terminal, a command can be made to run in background by appending it with &. So, I think you should be able to start both jars if you do something like -

java -jar /home/ec2-user/first.jar &
java -jar /home/ec2-user/second.jar

Aws Ec2 run script program at startup

A script can be passed in the User Data property.

If you are using the Amazon Linux AMI, and the first line of the script begins with #!, then the script will be executed the first time that the instance is started.

For details, see: Running Commands on Your Linux Instance at Launch

How to execute JAR on AWS EC2 instance from AWS Lambda

To run a command every time the server boots, you should add this entry to the server's crontab:

@reboot java -cp myJar.jar com.example.Main

You may need to provide the full paths to java, and the jar file in the command. Then your process to start and stop the instance is all that is needed, you will no longer need to automate the process of starting the Java process.

AWS EC2 instance not running Java program when launching

You don't have to put jar file in your user-date. First you have to create a script. You can create a script for each of the jar files. For response_server.jar you can create script java_response_server_launch.sh like this:

#! /usr/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/ec2-user/559/response_server.jar &
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"

Put your script under /etc/init.d directory, and then use the command:

update-rc.d java_response_server_launch.sh defaults

Also don't forget to chmod +x the script.

More on this you can refer

Tool for creating a Java daemon service on Linux

Running Commands on Your Linux Instance at Launch



Related Topics



Leave a reply



Submit