简体   繁体   中英

Linux Launch java program on startup (EC2 instance)

my server program needs to be launched on the startup of an EC2 instance. At the minute im just launching it from my SSH with the following commands:

 java -jar ~/DocumentManager/DocumentServer-0.2.jar  

I tried adding this to the.bashrc and /etc/rc.local files but they only seem to work when i ssh in.

Anyone know how to make it so an instance of my application is launched when the computer boots?

Thanks,

Ben

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.

Add ampersand(symbol '&') at the end of the command. For example, in your case, java -jar ~/DocumentManager/DocumentServer-0.2.jar &

Old question, but my answer might be helpful for people who look in future.

You can also run your program as a service which automatically run on ec2 container reboot. Below link worked for me:

https://medium.com/@lizlieholleza/run-your-java-application-as-a-service-in-an-ec2-instance-amazon-linux-d7c7b4c0b2f4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM