2

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

Ben Flowers
  • 1,434
  • 7
  • 21
  • 49

3 Answers3

2

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.

TOC
  • 4,326
  • 18
  • 21
  • Hi, I tried that and its not working. I ssh'd in and looked at the processes running and its not there. Do i need to put in a start script somewhere? – Ben Flowers Aug 04 '12 at 14:26
  • @BenFlowers : hello, yes under /etc/init.d directory – TOC Aug 04 '12 at 14:27
  • Okay so basically i created the java_server_launch.sh file and then ran the commands you said. Is there anything else i need to do? (still not working) thanks for your help btw – Ben Flowers Aug 04 '12 at 14:34
  • @BenFlowers : I edited my post, by the way you can take a look at /etc/init.d/skeleton file to see how you can use it to meet your need. – TOC Aug 04 '12 at 14:51
  • still not working, its close i can tell!! so my jar file is located at ~/DocumentManager/DocumentServer-0.2.jar and i am sshing in as ubuntu@(amazoninstance). im storing my script.sh file in /etc/init.d and it looks like this: #! /usr/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin JAVA=/usr/bin/java MY_SERVER=~/DocumentManager/DocumentServer-0.2.jar USER=ubuntu /bin/su - $USER -c "$JAVA -jar $MY_SERVER &" and running update-rc.d script.sh defaults any clue what im doing wrong? – Ben Flowers Aug 04 '12 at 15:18
  • FIXED IT!! just needed to chmod +x the script file D'OH! – Ben Flowers Aug 04 '12 at 15:28
  • never underestimate the stupidity of someone who has been trying to fix a small problem for 3 hours! Thanks again! – Ben Flowers Aug 04 '12 at 15:33
1

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.

Rajesh
  • 336
  • 3
  • 13
0

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

Rahul Babu
  • 730
  • 1
  • 5
  • 25