# rc.d Script for a program which need long time to close gracefully



## freebuser (Mar 29, 2012)

Hi,

I have recently migrated from Debian Wheezy to FreeBSD 9.0 Release. One of the programs I use is OpenKM which is a jboss based document management system. I was able to create an rc.d script to start this program on system startup and close the program on system shutdown. Startup works fine as there is no time limit on how long the program can take to start. But on shutdown the program couldn't close properly as the system shutdown is too fast.

I have included

```
# PROVIDES: Sopenkm java
# KEYWORD: shutdown
```
in rc.d script but no difference in the results.

Is there anything more I need to do to give the program enough time to close properly before the system shutdown?

Thanks heaps,


----------



## freebuser (Mar 29, 2012)

Complete : script

```
#!/bin/sh
# Sopenkm startup Script

# PROVIDE: Sopenkm java
# KEYWORD: shutdown

. /etc/rc.subr

name="Sopenkm"
rcvar=${name}_enable
Sopenkm_enable=${Sopenkm_enable-"NO"}

start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name

JAVA_HOME="/usr/local/openjdk6/";

export JAVA_HOME

Sopenkm_start(){
echo "Starting OpenKM....."
rm -r /mydata/jboss/server/default/work & rm -r /mydata/jboss/server/default/tmp & /usr/local/lib/libreoffice/program/soffice "-accept=socket,host=localhost,port=2222;urp;StarOffice.ServiceManager" -nologo -headless -nofirststartwizard & /mydata/jboss/bin/run.sh -b 0.0.0.0 &

}

Sopenkm_stop(){

echo "Stopping OpenKM....."
/mydata/jboss/bin/shutdown.sh -S
killall -9 soffice.bin &
}


run_rc_command "$1"
```

Cheers,


----------



## samu (Apr 1, 2012)

I think that doing [cmd=]killall -9 soffice.bin &[/cmd] can be the issue. You are sending a SIGKILL to the processes and detaching, so that the rest of shutdown goes on. 

I'm not sure if I understand it correctly, but if you use SIGKILL, it will not be given a chance to shutdown properly (if shutdown.sh didn't finish). Use a SIGTERM instead (if the software can catch it and perform a clean shutdown by itself). If I'm wrong, somebody please correct me.


----------



## freebuser (Jul 27, 2012)

The reason the script didn't shutdown properly was the java process is the one which actually started by the script not Openkm.

So adding

```
procname="java"
```
solved the problem.

My new script looks like this:

```
#!/bin/sh
# Openkm startup Script

# PROVIDE: Openkm
# REQUIRE: mysql LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="Openkm"
rcvar=${name}_enable
Openkm_enable=${Openkm_enable-"NO"}

start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name

JAVA_HOME="/usr/local/openjdk6/";

export JAVA_HOME

procname="java"

Openkm_start(){
echo "Starting OpenKM....."
rm -r /alldata/programs/jboss/server/default/work &
rm -r /alldata/programs/jboss/server/default/tmp &
/usr/local/lib/libreoffice/program/soffice "--accept=socket,host=localhost,port=2222;urp;StarOffice.ServiceManager" --nologo --headless --nofirststartwizard &
su -m nobody -c /alldata/programs/jboss/bin/run.sh -b 0.0.0.0 & > /dev/null

}

Openkm_stop(){

echo "Stopping OpenKM....."
/alldata/programs/jboss/bin/shutdown.sh -S
killall -9 soffice.bin &
}


run_rc_command "$1"
```


----------

