From owner-freebsd-rc@freebsd.org Mon Aug 20 12:17:56 2018 Return-Path: Delivered-To: freebsd-rc@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0BD49106B97B for ; Mon, 20 Aug 2018 12:17:56 +0000 (UTC) (envelope-from luke@solentwholesale.com) Received: from mail-qk0-x232.google.com (mail-qk0-x232.google.com [IPv6:2607:f8b0:400d:c09::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id A94848A9D7 for ; Mon, 20 Aug 2018 12:17:55 +0000 (UTC) (envelope-from luke@solentwholesale.com) Received: by mail-qk0-x232.google.com with SMTP id d15-v6so3098262qkc.1 for ; Mon, 20 Aug 2018 05:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=solentwholesale-com.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=V2jWi9vGTJDRCpixDKg8XEltmBZRDIYSoCiDymg3z5Q=; b=NzLt84dXWpSPKyL90wvYR4HkbXLevEydha9zsK6xC5cEPOxcAu3lTcQjYh62hoCSo1 fKpZcnMzf24ebAdExjyTnhNZu7SS3Y8DDBpHx+BjRFLqGxxvY0n2IiNCiUangypV+oZu OyC8MwgfE43rKKa5aOAzLuddAGo0vMQpqkhrXCJYd1WnHm5gJb5oglv+lRijC63jMBxl wpEmagysyRsLzXnrgI4hseegge3dt0eUDbKrjoTBKns7yQenOYrx1Fmk3Z4JncG+lAqT XXHo0eLV2+63QlZaJsUXkAhiEWUeoKq8p7S5QcNkuli/YJopMJP5gqBu5hIuJzCWt+su HgKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=V2jWi9vGTJDRCpixDKg8XEltmBZRDIYSoCiDymg3z5Q=; b=evY6WNm20BGpiI0V/Aydkf2ESx0qEcZZNFSa1crgEL/zNV72Rz3eh+ZHAj35QVdtfz 9uHRkGH7b4PZbf0Egw0s2fnnauuL6i6cnbC4KWo3AgLZb+Z+/et2lQ53qv7I4mw1ef+P dTOdkQzpKuq17w8m9fuH9wQal+ZrOZrauwEaugHT6F72nXlBFO5u7PsbV/fomDzIMExx eRgs44/wI1GKBbKvPKTzWklz/emvgde5b+0h79SvsP1MxSbC1BvKY56D3g0EskKudOOi GBf/0jxVpdw3CIxK7ex/HdURgT3Zw9c+SdKyh88Z1mX7lHpPB+70NmOr2d33QEPXzdeI uQnw== X-Gm-Message-State: APzg51AR/XDVeuCmy0wYegUUZMi6EqfTvfT4pwN+6gdbYLradYs+sZQb jRkbSM2NA7IBEUs52xe7Jm8S+koRI4KfAGQ3LiCmtNkmeb+PDQ== X-Google-Smtp-Source: ANB0VdY06wjUCIR/eeuKf2gl9URnWPQbgOJqCTCeXKDuGQNQT6RmySVcp8aPiApfpZ3FcYgxuH4C6S49vk3Hj0OzOLI= X-Received: by 2002:a37:d1cd:: with SMTP id o74-v6mr1522489qkl.227.1534767474508; Mon, 20 Aug 2018 05:17:54 -0700 (PDT) MIME-Version: 1.0 From: Luke Crooks Date: Mon, 20 Aug 2018 13:19:55 +0100 Message-ID: Subject: Custom Java based rc.d script To: freebsd-rc@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.27 X-BeenThere: freebsd-rc@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Discussion related to /etc/rc.d design and implementation." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Aug 2018 12:17:56 -0000 I am using an rc.d script, provided by then open source Project Traccar, the script provided is here: #!/bin/sh # # 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" # read configuration and set defaults load_rc_config "$name" : ${traccar_enable="NO"} : ${traccar_root="/opt/traccar"} # standard root : ${traccar_java="java"} # path to your JRE : ${traccar_user="traccar"} # user to run as : ${traccar_stdout="/dev/null"} : ${traccar_stderr="/dev/null"} traccar_chdir=${traccar_root} # will add a cd $traccar_root before launching command="$traccar_java" command_args="-jar ${traccar_root}/tracker-server.jar ${traccar_root}/conf/traccar.xml" pidfile="/var/run/${name}.pid" required_files="${traccar_root}/conf/traccar.xml" # ugly hack to abuse this but I don't want to launch with nohup around su start_cmd="/usr/sbin/daemon -p ${pidfile} -u ${traccar_user} ${command} ${command_args} > ${traccar_stdout} 2> ${traccar_stderr}" run_rc_command "$1" 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. Luke Crooks crooksey