PDA

View Full Version : Launch application on server startup



waldy
23rd November 2004, 01:07 PM
Hello all,

I have a fedora 2 server thats up in the loft, so its not easy to get to. I hava a java application that I would like to run automatically when the server is re-started remotly.

The app can be started from within gnome by issuing java -jar app1.jar. I've tried vncserver & winaxe on a remote pc to start the app but of course the session ends when I close the remote X session.

thanks

Waldy

crackers
23rd November 2004, 07:28 PM
There's a couple of ways you can do this:

1) Create an init-script and put it in /etc/rc.d/init - the other "system services" are typically started from there. You can use several of these as templates for building your own. It'll take a bit of research to do this, but it's well worth it.

2) Put the command in /etc/rc.d/rc.local. Actually, what you should do is create a separate shell script to run it. Here's an example:


#!/bin/sh
JAVA=/path/to/java/bin/java
JAR=/path/to/app1.jar
USER=nobody
su -c "nohup $JAVA -jar $JAR > /dev/null 2>&1 &" $USER


What this script should do is run the program in the background (I'm assuming this is not a UI-based application as that is a completely different issue) as the designated user and trash any console output. Do not run "services" as root if at all possible, as that opens up likely avenues for expolits, especially if the service uses any TCP or UDP ports.

waldy
24th November 2004, 12:33 PM
Hi, Thanks for the reply, looks like just what I need.
Having trouble running the bash script though (apologies, I'm a bit new to Linux)

I've slightly simplified your example script as follows:

User=walders
java=/usr/java/jre1.5.0/bin/java
jar=/usr/local/slimserver/HTML/EN/html/softqueeze/MiniSqueeze.jar
su -c "$java -jar $jar" $User

Running the shell script from root I get:

bash: /root/.bashrc: Permission denied
Unable to access jarfile /usr/local/slimserver/HTML/EN/html/softqueeze/MiniSqueeze.jar

however with my user walders I can run this jarfile OK

I figure that running this shell as root user is a good way of testing it before putting it in rc.local?

thanks Waldy

crackers
24th November 2004, 06:18 PM
Actually, it's "designed" to be run by root, since that's how services get started.

On to the problem: can you run the shell script as your usual user? (I have yet to figure out the first "warning" - but I haven't tried very hard since it's not impacted anything - so I just ignore it.)

By the way, it's customary in shell scripts to use all upper-case for environment variables - makes 'em easier to see, especially when not using a colorizing editor.

waldy
25th November 2004, 11:56 AM
Hi , thanks for the tips.

So far no luck with my script. I've echoed the variables to see if they are right, seem to be. Also I've done an chmod +x scriptname.


#!/bin/sh
JAVA=/usr/java/jre1.5.0/bin/java
JAR=/usr/local/slimserver/HTML/EN/html/softsqueeze/MiniSqueeze.jar
USER=walders
#$JAVA -jar $JAR
echo "$JAVA"
echo "$JAR"
echo "$USER"
su -c "$JAVA -jar $JAR" $USER


The reply I get when I run from the user is:

/usr/local/slimserver/HTML/EN/html/softsqueeze/MiniSqueeze.jar
walders
does not exist

I run it by sh ./scriptname

waldy
25th November 2004, 01:50 PM
Not sure what went on before, however, I put a symbolic link to java in /usr/local/bin & the script now runs!

Also I can run it directly ie ./scriptname

I'll have a try at putting it into rc.local this evening, might be onto a winner now :)

Report back tomorrow

Waldy

waldy
25th November 2004, 03:37 PM
Nearly there now I think.

I can run the script from the user however when I su to root & try to run it I get the following error

Xlib: connection to ":1.0" refused by server
Xlib: No protocol specified

Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':1.0' as the value of the
DISPLAY variable.

Now, I have a remote xsession using vnc which might account for it, however when I put the script in rc.local the app doesn't run, so I guess its stumbling.

Any thoughts on this last hurdle?

best wishes

Waldy

waldy
25th November 2004, 05:00 PM
Don't mind me I'm thinking out loud!

It looks like an xsession problem running under root. ie it won't

I'll look at the application which should run headless (ie no output) when I get a chance, & hopefully it will work then


regards

Waldy

crackers
26th November 2004, 02:05 AM
There's a command-line option to run headless, but I don't remember what it is. If the program uses anything from AWT, Swing, Graphics2D, etc. you'll probably have to use that option to "force" headless mode.

waldy
26th November 2004, 10:42 AM
Thanks a million,


Managed to run the app softsqueeze in headless mode. For anyone interested I used:

su -c "$JAVA -Dserver=127.0.0.1 -Dserverversion=5.4 -Dskins=headless -jar $JAR > /dev/null2>&1 &" $USER

Placed that in rc.local & all works a treat now :)

best wishes

Waldy

davened
4th July 2005, 08:52 AM
Hi, I am trying to also run softsqueeze but since I am having problems with the JDK and GJC I cannot seem to run it..

Getting this error "No X11 DISPLAY variable was set, ................"

I will do some more research but since I renamed the /usr/bin/java link which went to "ALTERNATIVES" to use the /usr/jdk1.5 .../bin/java I think I got one step closer.

Anyway, Need help in setting up this Fedora core 3 Softsquueze as the music server.

Any help with this X display problem would be appreciated.

The post above doesn't work for me...