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?

share|improve this question

closed as off topic by Stephen C, rolve, Bali C, Ed Heal, Jean-François Corbett Dec 7 '12 at 9:14

Questions on Stack Overflow are expected to relate to programming within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Should be moved to askubuntu.com – Ocaso Protal Dec 1 '11 at 10:13
1  
or move to superuser – Chuck Dries May 19 '16 at 6:26
up vote 64 down vote accepted

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.

share|improve this answer
    
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 1 '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 1 '11 at 13:33
    
rc-update does not exist in Ubuntu. Use update-rc.d <basename> start|stop NN runlevel [runlevel] instead. Replace <basename> with your script name, NN with the order in which the script runs (within the specified runlevel). See 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.