Starting a Java program on system startup
I have written a small Java utility that my employer would like to run...but it has to be started at boot time and it has to stay running.
I have tried starting it both via inittab (which I have to admit is a little weird, we called a script that starts it) and by starting it in rc.local. The best we have seen is that, when the system boots, it does start up. But, when the login to the user's environment, it is gone.
It's just a simple little Window frame with three buttons, each button issues a different shell command string. If I start it manually it runs fine, hangs around on screen as long as I want, and when I click the button it does what I want. I just can't get it to start at system startup time.
Any hints would most certainly be appreciated.
Your graphic application is running on top of the X11 server.
The X server being restarted between user sessions, your program just can't survive.
You'd rather have it being launched each time users start their graphic environment, not once when the machine starts.
When you say you want it run at start up, do you mean each time you log in? If you wish it to be running when you are logged in, and you wish to see it, and it has a graphical window (X), it can only last as long as your X session. AFAIK, programs with windows cannot persist accros a login/logout session change, although I could be wrong. If you just want the program running whenever you log in, check out your desktop environment for session management facilities or some autorun function, e.g. in KDE the ~/.kde/Autostart directory.
If it can be run without a GUI, you should be able to run it from rc.local or inittab. rc.local is probably the correct place - be wary of putting things in inittab unless you absolutely have to. You get the respawn-on-crash mechanism there, but if you mess up your inittab, you can make a big mess which is a pain to sort out.
If the program will not run without a GUI, and if you do not need to see or interact with the program's GUI, consider running it using a "fake" X server, such as Xvfb. This can be run in the background and will not be affected by users logging in and logging out. You will not however be able to see or interact with the program on your main display.
The original program that I am replacing is started out of inittab and gets respawned. So essentially I suppose that it might actually die between sessions but be respawned again for the new session. The Java command does not accept a -display parameter to get the display window specified, so I did all of that in a script and called the script in inittab. This isn't very elegant and isn't particularly working well either. What I discovered a little later that, with it set up in this manner (and yes it is set to respawn), when I log on, the window for the Java prog is not there. WHen I exit out of the ssh window that I came in on..... about 10 of them pop up. So respawn is obviously respawning more than the once I need... I am obviously doing this in a manner that isn't reflecting that the JVM is up and running the program properly.
What I did in the shell is this
(ps.... I know this looks really bad in terms of form, but for now I am stuck. We cant
change everything and it really will run this way... I hope that you can try to see through that....)
/usr/src/java/j2sdk1.4.2/bin/java -classpath /usr/src/java/j2sdk1.4.2/bin ThreeButtonControl
this is in a script called init.java
when the system is first loaded, it runs it.
After I do the X login, it disappears.
I come in through an ssh, get in to /etc, call init.java and it pops up just fine and stays there. I cancel it. I log out.... and KABAM. About ten of them appear.....all at once... and..if I hit a button they work.
Did I mention that I love my job?
If you only want this program to run when you are logged into the system, do not run it from inittab, it is totally the wrong approach. inittab is not for user programs - only for daemons. The proper place is in your desktop environment startup files.
What desktop environment do you use, KDE, Gnome, XFCE, other?
Well....it isnt really a user...per se
Their design, as it sits, is to boot the RHEL4 box up as a dumb xterminal waiting on the main simulation machine(SGI). When the SGI detects the RH box, it starts up a login process of it's own, but sets the DISPLAY to the RHEL linux box. SO you get a display on the Linux system, you log in on the linux system, but you are actually logging in to the SGI. Essentially the Linux/RHEL4 box is just a big X terminal. And again, the program we are replacing works fine out of inittab. They have reasons for wanting to replace it and the program I have running is what they want. Again, I know that, if I start it locally, it will run. But no one is running on the local box. No one is logged in.... the way they have it set up, no one ever logs in on the RH/Linux box.
That said, I really appreciate the sentiment but this is a working system, been deployed for two years, we just want to take out one old process and put in one new one in it's place.
As already stated several times. Using startup scripts for what you want to achieve is the wrong approach, and using inittab is even worse. Your application isn't a daemon nor a one shot startup script.
As your goal is simply to have that application window to appear each time the X server is (re)started, just add your command to whatever Xsession launch script your system is using.
|All times are GMT -5. The time now is 12:45 PM.|