SOLVED Need help understanding jail startup scripts

chravis

Contributor
Joined
Jan 27, 2019
Messages
104
Greetings all -
I've read lots of posts already so I think I understand different ways of starting up a script or program when the jail starts. In my case, I am wanting to start a simple java program (in a jar file) that is basically like a chat server.
I have figured out how to start the program in various ways:

These are my baby steps to figure this out because I'm quite new at this particular activity:

Step 1
Make sure the program can run inside the jail outside of any kind of start up mechanism. After installing the jdk version I needed, and after putting my jar file into the jail via FTP, I am able to run the following successfully:
Code:
java -jar MessageServer.jar


I can also write a script to start the program successfully (startmessageserver.sh):
Code:
#!/bin/sh

/usr/local/bin/java -jar /root/MessageServer.jar


Step 2
Try to get it to run when the jail starts up. I was able to get this working using both (either) an rc.local file or an rc.d script. In either case, I can call the jar execution directly or call the startmessageserver.sh script. Both of these work, as I am able to connect to my server from a client machine.

My problem, however, is that when I start the jail it just sits there. If I start the jail through the GUI, I get the popup that says "Starting job" and it sits there for ever. It has started my program successfully, but the screen sits there forever.

I thought maybe my problem has to do with maybe it's waiting for output? So I changed my commands to something like this:

Code:
#!/bin/sh

/root/startmessageserver.sh >/dev/null 2>&1


But that doesn't seem to work either. I feel like I'm missing something fundamental but I'm not sure what it is.
 
Joined
Oct 22, 2019
Messages
3,641
My problem, however, is that when I start the jail it just sits there. If I start the jail through the GUI, I get the popup that says "Starting job" and it sits there for ever. It has started my program successfully, but the screen sits there forever.

So your jail starts successfully, and your script automatically runs, but the issue is that the GUI will not refresh? If you try to start the jail from the command-line using the "iocage" command, does it work without issue?
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Possibly there is an "&" missing at the end of that startup command. If you don't want to write a proper rcng script, you can put your commands into /etc/rc.local.

Another method is to use supervisord.
 

chravis

Contributor
Joined
Jan 27, 2019
Messages
104
So your jail starts successfully, and your script automatically runs, but the issue is that the GUI will not refresh? If you try to start the jail from the command-line using the "iocage" command, does it work without issue?
Yes, if I remove my script from the rc.d folder and start up the jail using iocage, it starts normally and I get the command prompt as expected:
1677860544825.png

However, once I put the script back into the rc.d folder, it hangs before "Starting services", but it does execute my program because I'm able to hit the server from a client machine.
1677860622346.png

FWIW - my jail is also named "messageserver" so don't let that confuse you.

This is my first time messing with rc.d scripts so it's possible I've done something wrong there, but this is what my file looks like:

Code:
#!/bin/sh

. /etc/rc.subr

name="messageserver"
start_cmd="${name}_start"
stop_cmd=":"

messageserver_start()
{
    /usr/local/bin/java -jar /root/MessageServer.jar >/dev/null 2>&1
}

load_rc_config $name
run_rc_command "$1"
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
The problem seems to be that your program does not background itself. So instead of /usr/local/bin/java -jar /root/MessageServer.jar >/dev/null 2>&1 you should probably write /usr/local/bin/java -jar /root/MessageServer.jar >/dev/null 2>&1 &

That's what I meant by the missing "&" in my first post.
 

chravis

Contributor
Joined
Jan 27, 2019
Messages
104
The problem seems to be that your program does not background itself. So instead of /usr/local/bin/java -jar /root/MessageServer.jar >/dev/null 2>&1 you should probably write /usr/local/bin/java -jar /root/MessageServer.jar >/dev/null 2>&1 &

That's what I meant by the missing "&" in my first post.
That is brilliant! I had no idea so thank you for explaining that in more detail. It works as expected now.
 
Top