4

I have a requirement where I need to develop application that reads TCP/IP Socket. I successfully made the program as Java program with No GUI means as soon as program runs it starts listening to the Socket and gets the response with the help of Netbeans IDE. Now as per my requirement i have to start execution of this program as soon as Linux system Booted.

Actually I am very novice in Java and Linux Platform, so have few doubts..

  1. Is my Socket Program with no GUI is fine to be run as per my requirement.
  2. How can I write script to run jar on Linux Boot up, I got to know. init.d is meant for this.
2
  • /etc/rc.local is the lazy place to put code to run on system start. Commented Mar 28, 2014 at 11:58
  • @Paul - Nice of you to offer an alternative Commented Mar 28, 2014 at 12:12

2 Answers 2

8

Ideally you should create a service wrapper for your java application and then make this service run on startup example here.

Use
sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu

Sign up to request clarification or add additional context in comments.

5 Comments

What if i wont change PID_PATH_NAME=/tmp/MyService-pid in your program.If i let it be as it is.Will it be OK
one More Silly question.Am sorry sir i am very novice to Linux ..How to come out of this command sudo vi /etc/init.d/mytestserv after pasting the script and execute next command i.e sudo chmod +x /etc/init.d/mytestserv
you mean getting out the vi editor!!?? press "ESC + :wq + "INTRO" to exit. If you mean to reboot the machine try "sudo reboot now" or "sudo reboot"
Got it sir..One More thing if my service is in started Mode and after some time i rebooted my Linux System then My Service will automatically be Started.Am i Right?
Yes Reboot you computer if you can to double check it. Lot's of luck!
2

So two things you'll need to do:

First create a small shell script to start your java program from a terminal. As you have packaged as a jar have a look at this, specifically the JAR Files as Applications section.

This may be sufficient: (although you'll want to use the full path to Java)

#!/bin/bash
java -jar path_to_jar_file

You should be able to run your script and successfully start your program.

Once you've got it starting from a script you can use standard linux tools to start the script. Either putting it in /etc/rc.local, or as you're using Ubuntu, use update-rc.d to start it on boot. See here for a very simple example of using update-rc.d

Hope this helps,

Will

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.