Thanks Raedwald. I studied the post you mentioned and I finally resolved it.
Here the steps I did.
startapp.sh
#!/bin/bash
java -jar /home/usr/local/bin/vedioplayer.jar
created the above script and saved it in /etc/init.d make sure you allow execution of the shell script.
sudo chmod +x /etc/init.d/startapp.sh
After that run the follwing command
sudo update-rc.d startapp.sh defaults 98 02
Also you can add the script to Startup Applications list from Applications.
Answer from Rajith Pemabandu on Stack Overflowshell script - Executing .jar File on Startup - Unix & Linux Stack Exchange
ubuntu - Make a "java -jar file.jar" run at startup - Unix & Linux Stack Exchange
How to run a JAR file on startup? (Ubuntu 13.10)
bash - run jar-file on startup linux - Stack Overflow
Here's a easy way to do that using SysVInit. Instructions:
Create the start and the stop script of your application. Put it on some directory, in our example is:
- Start Script:
/usr/local/bin/myapp-start.sh - Stop Script:
/usr/local/bin/myapp-stop.sh
Each one will provide the instructions to run/stop the app. For instance the
myapp-start.shcontent can be as simple as the following:#!/bin/bash java -jar myapp.jarFor the stop script it can be something like this:
#!/bin/bash # Grabs and kill a process from the pidlist that has the word myapp pid=`ps aux | grep myapp | awk '{print $2}'` kill -9 $pid- Start Script:
Create the following script (
myscript) and put it on/etc/init.d./etc/init.d/myscriptcontent:#!/bin/bash # MyApp # # description: bla bla case $1 in start) /bin/bash /usr/local/bin/myapp-start.sh ;; stop) /bin/bash /usr/local/bin/myapp-stop.sh ;; restart) /bin/bash /usr/local/bin/myapp-stop.sh /bin/bash /usr/local/bin/myapp-start.sh ;; esac exit 0Put the script to start with the system (using SysV). Just run the following command (as root):
update-rc.d myscript defaults
PS: I know that Upstart is great and bla bla, but I preffer the old SysV init system.
Yes! It is possible. :) Upstart is the way to go to make sure the service stays running. It has five packages, all installed by default:
- Upstart init daemon and initctl utility
- upstart-logd provides the logd daemon and job definition file for logd service
- upstart-compat-sysv provides job definition files for the rc tasks and the reboot, runlevel, shutdown, and telinit tools that provide compatibility with SysVinit
- startup-tasks provides job definition files for system startup tasks
- system-services provides job definition files for tty services
The learning is very enjoyable and well worth it. Upstart has a website: http://upstart.ubuntu.com/
I use RemoteDroid, which allows you to use an Android device for input. (My Ubuntu box is my TV PC). I downloaded the JRE and got the .jar file to execute without issues, but getting it to start on boot is seemingly impossible. (I'm just simply trying to avoid having to plug a keyboard/mouse in just to start the program.)
I've scoured the web finding a myriad of different solutions; some adding additional software, some using UpStart (which I'm not too familiar with), all of which seem to be extremely complex (and obviously not working for me). There's gotta be a simpler way to just run this .jar on startup without a bunch of hacks.
I think that if you want to run the application as a daemon, the best solution is using Apache Jsvc :
Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily. Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user. It can run on Win32 via the Cygwin emulation layer, however Win32 users may prefer to use procrun instead, which allows the application to run as a Windows Service.
Approach 1 should work but you must run the java process in the background by adding & to the command.
But there are better solutions to run java apps as a deamon. The above can be considered as a quick fix.
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
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
Regrettably, I don't believe there is a single, universal way to accomplish exactly what you want. Each nix has an init system, and there are various different init systems out there (SysV, Upstart, systemd, etc.). Now, systemd has some support for running SysV daemons, so you might be able to get away with writing a SysV script and have it work for both, but I do not know how that will fare with Upstart and other systems. I can see two courses of action that may yet help you accomplish your goal:
Write a service for each major init system (note that many distros are switching to systemd, so it may be a good one to start with).
Give up on running it at boot and try to run it a little later.
For example, though init systems are still pretty fragmented, shell login is pretty much universal. You could add a couple lines to each $HOME/.shellrc running the jar if it hasn't already been run. Doing so would be much less work and much more universally applicable.
The obvious advantage of 1 is that it would run the jar before users see anything. However, this can be mitigated in 2 by setting up autologin (though not a great idea for security).
Note that, if this is a GUI jar, then you're going to have a fair number of extra headaches (making X, or Wayland start so that your jar can run will add a few lines, at least, to whatever solution you decide).
There is no universal method for creating a system service on all GNU/Linux distros, since they use a variety of init systems. Once upon a time they all used more or less the same SysV style init (excepting some which used a more BSD style system), which allowed for the writing of generic init scripts that would require little modification from distro to distro.
Currently, SysV is still used by a number of distros, such as Debian and RHEL / CentOS. However, the newer init systems -- systemd (Fedora, Arch, et. al.) and upstart (Ubuntu)1 do include mechanisms for backward support of SysV style init scrips, so if you are looking for the method most easily adopted for use on the most systems, that is still it.
Keep in mind that "linux" is not an operating system in the sense that "Windows 8" or "OSX" are operating systems. Linux is an OS kernel used on a wide variety of platforms (e.g., Android); colloquially "GNU/Linux" refers to the collection of OS distributions discussed above, but these are not all the same. There is no serious intention or desire to unify these, just as there is no serious intention or desire to unify Windows and OSX so that, e.g., someone could ask "How can I run my jar file at start up on both OSX and Windows?" -- you are out of luck, you will need to package it separately for each of them. That is, in fact, how software in the linux world generally is distributed; there are separate packages for each and every distro.
1 BUT Ubuntu (and Debian) are moving to systemd, meaning upstart will likely disappear, and most of the major distros will have a common init system.