66

I want to run some Java programs in the background when the system boots in Ubuntu. I have tried to add a script in /etc/init.d directory but failed to start a program. i.e programs are not started. What should I do for that?

Manjula
  • 4,961
  • 3
  • 28
  • 41
Anand Soni
  • 5,070
  • 11
  • 50
  • 101

1 Answers1

95

First of all, the easiest way to run things at startup is to add them to the file /etc/rc.local.

Another simple way is to use @reboot in your crontab. Read the cron manpage for details.

However, if you want to do things properly, in addition to adding a script to /etc/init.d you need to tell ubuntu when the script should be run and with what parameters. This is done with the command update-rc.d which creates a symlink from some of the /etc/rc* directories to your script. So, you'd need to do something like:

update-rc.d yourscriptname start 2

However, real init scripts should be able to handle a variety of command line options and otherwise integrate to the startup process. The file /etc/init.d/README has some details and further pointers.

c.hill
  • 3,127
  • 25
  • 31
opqdonut
  • 5,119
  • 22
  • 25
  • But where should my java file placed? I have created simple java program with infinite loop. then javac and java command put in /etc/rc.local then I have reboot my system but program was not running. what to do? – Anand Soni Dec 01 '11 at 11:39
  • Thanks its being done. I have created a service in /etc/init.d and then started service in rc.local. – Anand Soni Dec 01 '11 at 13:33
  • `rc-update` does not exist in Ubuntu. Use `update-rc.d start|stop NN runlevel [runlevel]` instead. Replace `` with your script name, `NN` with the order in which the script runs (within the specified runlevel). See http://en.wikipedia.org/wiki/Runlevel#Debian_Linux for more info about run levels in Ubuntu/Debian – c.hill May 28 '14 at 15:06
  • Or `update-rc.d yourscriptname defaults` to create both startup and kill scripts all at once. – ychaouche Jan 20 '15 at 12:47
  • I don't see a mention of 'boot' anywhere on both cron's and crontab's man pages. – Rani Kheir Mar 16 '17 at 18:10
  • For people who need to startup apps at boot-up... Check [this](https://askubuntu.com/a/140713/668801) out! – Niket Pathak Aug 01 '17 at 10:14