I have a jar file which I need to run at startup in all distros of Linux. My previous question here, gave me an idea a rough idea on X-servers. Since I wasn't able to perform startup, I moved on to the idea of adding a .desktop file to /etc/xdg/autostart. This works for ubuntu and I am currently testing it in Linux Mint both cinnamon and mate versions. I did a small research for other distros but they don't seem to have the /etc/xdg/autostart instead they have /xdg-autostart but I need to run my jar file in all distros of Linux. I tried crontab but @reboot didn't work in ubuntu 14.04 for me. Another problem is I need to remove the file I am placing to startup when I uninstall the jar. If I edit rc.local, I won't be able to revert the edit. Is there a common way in which I can do startup in Linux

  • The XDG autostart (X server based) mechanism is fundamentally different from the rc.local (init based) one. The former starts applications inside per user GUI sessions. The latter starts things on a system wide basis, and is not appropriate for GUI applications since GUI sessions are instantiated per logged in user. You need to differentiate and explain which of those two possibilities you want to exploit. – goldilocks May 28 '14 at 13:40
  • @goldilocks My jar file works without UI but at times when server sends message, a window will be opened with the message. – Joker May 29 '14 at 4:10

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.

  • Thanks for the explanation. Is there a way where I can identify the init systems in the distro using java and write a script accordingly? Because on running my jar file I need it to write the script and set to the respective area. If I am installing my jar file in say 500 systems, I cannot manually write a script in every system. If I find the init system and write the script, it would be a easier task. – Joker May 29 '14 at 12:52
  • 1
    I would not put everything in the jar or do the install using java; use a tarred package with a separate install script (i.e, the normative approach -- also this way the jar will be the same even if the install method differs). Vis. identifying the init system, that would be a good separate question of its own BUT I think if you really need to install to 500 machines, you should first find out what those machines are running and create a few different packages to cover them. When you hit a case that isn't covered, deal with it; you may end up with 3 or 4 different versions... – goldilocks May 29 '14 at 13:04
  • 1
    ...of the installer. Don't try to cover all (theoretical) situations. Start with actual ones. – goldilocks May 29 '14 at 13:05
  • I will try and suggest the tar package. But how can we determine where the script should be located when we use it? and as for the distros, I think mostly Ubuntu, Mint, Redhat and CentOS will be used. Correct me if I am wrong. – Joker May 29 '14 at 13:12
  • 1
    I just use the man pages, but if you look around you should be able to find some tutorials. Arch uses systemd and generally has a great wiki. However, note that using the init system (e.g. upstart, systemd) and using xdg are fundamentally different as explained in my first comment on your question! You probably want to stick to the same method on all systems... – goldilocks May 29 '14 at 13:31

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:

  1. 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).

  2. 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).

  • My jar is a background process but when the server sends a message, a JOptionPane message dialog will be displayed. Case 1: I should be able to find the init system used via java. Is it possible to find it via java or terminal? Case 2: I need the jar to run on startup automatically but an delay around 10 sec is acceptable. Even for that we will be writing a script to activate the jar right? $HOME/.shellrc isn't common to all distros. To my knowledge, shell can be bash or sh. I am a beginner so correct me if I am wrong. I need to find the shell and script accordingly right? – Joker May 29 '14 at 4:44
  • 2
    +1 There is no universal system. My advice would be: if you want to run a system wide service, start with one distro, or one init system, work it out there, and then consider the others. – goldilocks May 29 '14 at 10:40
  • @goldilocks I already tried it in ubuntu. I first tried crontab but @reboot did not work for me. So I switched to rc.local but the problem is I won't be able to remove the edited line using java while uninstalling the jar. So I searched some more and got to adding a .desktop file to /etc/xdg/autostart. This method was easy for me, as I can remove the .desktop files and script from /usr/bin during unistallation – Joker May 29 '14 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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