Problem with FreeBSD rc.d script

Luke Crooks6 years ago

The provided FreeBSD rc.d script taken from here:

https://www.traccar.org/freebsd/

Works, in so much that it attempts to start traccar, however, it fails to start as the command with the chdir does not seem to be executed:

traccar_chdir=${traccar_root}

As when I view the logs for why the daemon failed to start, I can see:

Exception in thread "main" java.io.FileNotFoundException: ./conf/default.xml (No such file or directory)

And if I edit the traccar.xml to provide the full location of /conf/default.xml it passes this stage (only to error further due to other relative imports in other files). Obviously changing the file path's in the traccar config is not feasible, but it proves that the error is the rc.d script not executing the chdir.

If I run the script with debugging support I get the following output https://paste.ubuntu.com/p/Bv64c7BzY9/ (too long to paste here).

So then I tried to re-write the rc.d script, based off of other Java style scripts, and got this:

#!/bin/sh

## Service for traccar.

# PROVIDE: traccar
# REQUIRE: DAEMON
# BEFORE:
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable `traccar':
#
# traccar_enable="YES"
#

. /etc/rc.subr

name="traccar"
rcvar=traccar_enable
pidfile_child="/var/run/${name}.pid"
pidfile="/var/run/${name}_daemon.pid"

traccar_chdir="/usr/local/traccar"


command="/usr/sbin/daemon"
start_precmd="${name}_prestart"
procname="traccar"

load_rc_config "$name"
: ${traccar_enable="NO"}
: ${traccar_root="/usr/local/traccar"}                    # standard root
: ${traccar_java="/usr/local/openjdk7-jre/bin/java"}    # path to your JRE
: ${traccar_user="root"}                        # user to run as
: ${traccar_stdout="/var/log/traccar_running.log"}
: ${traccar_stderr="/var/log/traccar_error.log"}

traccar_chdir=${traccar_root}                   # will add a cd $traccar_root before launching
command_args="-jar ${traccar_root}/tracker-server.jar ${traccar_root}/conf/traccar.xml"


traccar_prestart() {

    # set the daemon / java flags
    rc_flags="-r -P ${pidfile} -p ${pidfile_child} ${traccar_java} ${command_args} >> ${traccar_stdout} 2>&1 ${rc_flags}"
}

traccar_describe() {
    echo "Traccar started..."
}
run_rc_command "$1"

Now, this script starts the service, and correctly starts it when the machine is powered on.

However, it doesn't show as running...

# service traccar status
traccar is not running.

So I check the output of the PID files:

# cat /var/run/traccar.pid
50137
# cat /var/run/traccar_daemon.pid
49060

And if I can see those PID's here:

root@traccar:~ # ps aux
USER    PID %CPU %MEM     VSZ    RSS TT  STAT STARTED    TIME COMMAND
root  48997  0.0  0.0   10492   2444  -  IsJ  09:57   0:00.36 /usr/sbin/syslogd -s
root  49060  0.0  0.0   10468   2052  -  IsJ  09:57   0:00.00 daemon: /usr/local/openjdk7-jre/bin/java[50137] (daemon)
root  49103  0.0  0.0   20628   6260  -  SsJ  09:57   0:00.04 sendmail: accepting connections (sendmail)
smmsp 49114  0.0  0.0   20628   6072  -  IsJ  09:57   0:00.00 sendmail: Queue runner@00:30:00 for /var/spool/clientmqueue (sendmail)
root  49122  0.0  0.0   12592   2340  -  SsJ  09:57   0:00.01 /usr/sbin/cron -s
root  50137  0.0  1.3 4673476 418304  -  IJ   10:47   0:15.86 /usr/local/openjdk7-jre/bin/java -jar /usr/local/traccar/tracker-server.jar /usr/local/traccar/conf/traccar.xml -jar /usr/local/traccar/tracker-serv

And if I run kill on 50137 (the java process, which is also the same number returned from /var/run/traccar.pid) the deamon correctly brings it back up.

So my new script half works, it brings the application up, but doesn't show it as running from the service command, and because of this I cannot use

# service traccar restart
traccar not running? (check /var/run/traccar_daemon.pid)

Where as above, we know the PID returned from /var/run/traccar_daemon.pid is running and shows from ps output.

I think I am almost there, if anyone can help/advise what might be wrong, I would be very grateful, thanks.