How to create a service for ubuntu to run java code for auto-launch at reboot?

Posted on 2011-05-11
Medium Priority
Last Modified: 2012-05-11
Hi there,

I got a ubundu desktop.  I would like to start a java program at reboot.
I put some lines in rc.local like

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user_name/code_dir
cd /home/user_name/code_dir
java run_code

#run_code is a java class
#export command is needed
#cd command to change directory before launching is needed

After reboot without user login, java code is not up and running yet.  
(java code is a network application and I cannot communicate with
java code if it's not running.)  But after login, java code is running.

I'm thinking to use service for auto-launch instead of rc.local.  But
I need help on that task.   And the main issue to launch my java code
automatically at machine reboot without login.  Other ways to accomplish
that purpose are fine, too.

All suggestions are appreciated.
Question by:asugri
  • 6
  • 4
LVL 31

Expert Comment

ID: 35743333
In Ubuntu you need /etc/init.d/local.  Make sure it is executable.  Copy all the stuff from rc.local and try again
LVL 38

Expert Comment

ID: 35743371
> java run_code
Besides, need to give the full path to java, such as
/usr/java/jdk/bin/java run_code
(use your own java path)

Author Comment

ID: 35743489
Thanks to  farzanj and wesly.

The tips did not help.

I placed those lines into /etc/init.d/local  and ran chmod 755.
/usr/bin/java is used to launch the code.

The code even did not start at all.  
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 38

Expert Comment

ID: 35743513
Please put it into a script
-----/home/<user>/bin/script1.sh --
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user_name/code_dir
cd /home/user_name/code_dir
/usr/bin/java run_code

chmod 755 /home/<user>/bin/script1.sh

And put
/home/<user>/bin/script1.sh  > /tmp/start.log 2>&1

and reboot the system (wait for 5 minutes and check /tmp/start.log)

Author Comment

ID: 35748314
Hi Wesly,

I did what you suggested but the code is still not launched and
there is no start.log created under /tmp.

Before I submit, I checked again and everything is what you said
(one line in /etc/init.d/local  .........)

LVL 38

Expert Comment

ID: 35748512
sudo update-rc.d local defaults 80
LVL 38

Accepted Solution

wesly_chen earned 2000 total points
ID: 35748530
Or you can put /home/<user>/bin/script1.sh to /etc/init.d

sudo cp -p /home/<user>/bin/script1.sh /etc/init.d
sudo chmod +x /etc/init.d/script1.sh
sudo update-rc.d script.sh defaults

Author Comment

ID: 35749311

Thanks a lot for the help.

with "sudo update-rc.d local defaults 80 ", java code ran after reboot and login.
I waited for 5 minutes after reboot, test access from a different machine to
java code (a network application) failed.   But after login, test access worked.

However, I'm looking for is an auto launch at reboot without the need of login.
LVL 38

Expert Comment

ID: 35749519
Whatever command/script you put in /etc/init.d/local is run at boot up without login.
add the following line into /etc/init.d/local
echo "test" | /bin/mail -s "boot run test" [your email address]
then reboot to see if you  receive email or not.
First, test
echo "test" | /bin/mail -s "boot run test" [your email address]
from terminal command to make sure send out mail work.

> But after login, test access worked.
What is your run_code do?

Author Comment

ID: 35749798

My java code is some kind of server application using java ServerSocket.
The test acceess is the client contacting the server application from
another machine.

My Ubuntu desktop does not have /bin/mail.  I just checked ubuntu pages
on the web and there are some mail servers available in main repository.
I'm sure how difficult to install/configure mail server, but may try
later this week or in the weekend.

LVL 38

Expert Comment

ID: 35749917
Here is quick guide for mail client package installation

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question