I am trying to write an rc.d script to start and stop a (Python) script. Starting the command seems to be working, but stopping it is not yet the way I want.
My goal is to ensure the script receives a SIGTERM signal when it should shut down; that seems not to be the case; I think only the daemon process is receiving the signal.
I used Practical rc.d scripting and Supervised FreeBSD rc.d script for a Go daemon as a reference when starting out to write the rc.d script.
Here is what I have come up with so far:
I have tried to "play" with the
The problem seems to be that the PID that ends up in
How can I get daemon to "forward" the SIGTERM signal to my script?
EDIT:
To be exact: the script is terminated by issuing the
When the script is running, ps shows:
And when I send the SIGTERM signal to PID 13454 I see the signal hander of my script fire. When using
My goal is to ensure the script receives a SIGTERM signal when it should shut down; that seems not to be the case; I think only the daemon process is receiving the signal.
I used Practical rc.d scripting and Supervised FreeBSD rc.d script for a Go daemon as a reference when starting out to write the rc.d script.
Here is what I have come up with so far:
Bash:
#!/bin/sh
#
# PROVIDE: pmm
# REQUIRE: networking
# KEYWORD:
#set -v
#set -x
. /etc/rc.subr
name="pmm"
rcvar="${name}_enable"
pmm_user="memyselfandi"
pmm_command="/usr/home/memyselfandi/.virtualenvs/pmm-test/bin/pmm" # NOT an rc.subr thingie, regular var
pidfile="/usr/local/var/run/${name}.pid"
command="/usr/sbin/daemon"
procname="daemon:" # needed?
start_precmd="${name}_prestart"
load_rc_config $name
: ${pmm_enable:=no}
: ${pmm_config_file:=/usr/home/memyselfandi/etc/pmm_conf.py}
: ${pmm_log_file=""}
# command and command_args only used for "start", I think?
# command_args will be added to the command line after $pmm_flags
# do NOT add dashed options to command_args, use prestart and rc_flags, see
# https://www.freebsd.org/doc/en/articles/rc-scripting/article.html#rc-flags
#command_args="-P ${pidfile} -r -S -T pmm -o /tmp/pmm.log ${pmm_command} start --config-file=${pmm_config_file}"
pmm_prestart() {
if [ -z "${pmm_log_file}" ]; then
# pmm_log_file is empty/not set
rc_flags="-P ${pidfile} -r -S -T ${name} ${pmm_command} ingest start --config-file=${pmm_config_file}"
else
rc_flags="-P ${pidfile} -r -o ${pmm_log_file} ${pmm_command} ingest start --config-file=${pmm_config_file}"
fi
debug "using config file: ${pmm_config_file}"
}
pmm_deep_status() {
echo "Status:"
${pmm_command} status --config-file=${pmm_config_file}
echo "Status done"
}
run_rc_command "$1"
I have tried to "play" with the
-P/-p
flag of daemon() to no avail.The problem seems to be that the PID that ends up in
PIDFILE
(obviously) is the PID of the daemon() utility, not that of my script, and when the stop
command is issued only that daemon PID is sent the SIGTERM signal, but not my script (which has a different PID).How can I get daemon to "forward" the SIGTERM signal to my script?
EDIT:
To be exact: the script is terminated by issuing the
stop
command, it is just not receiving the SIGTERM signal.When the script is running, ps shows:
Code:
# ps aux | grep pmm-test
memyselfandi 13454 3.0 0.2 49964 40944 - S 10:42 0:00.85 /usr/home/memyselfandi/.virtualenvs/pmm-test/bin/python /usr/home/memyselfandi/.virtualenvs/pmm-test/bin/pmm ingest start --config-file=/usr/home/memyselfandi/etc/pmm_conf.py (python3.6)
memyselfandi 13453 0.0 0.0 11004 2392 - Ss 10:42 0:00.00 daemon: /usr/home/memyselfandi/.virtualenvs/pmm-test/bin/pmm[13454] (daemon)
# service pmm onestatus
pmm is running as pid 13453.
And when I send the SIGTERM signal to PID 13454 I see the signal hander of my script fire. When using
stop
it does not (which is the same if I send the SIGTERM signal to 13453).