Home > Net >  FreeBSD custom service prints stdout to shell
FreeBSD custom service prints stdout to shell

Time:09-11

How do I stop a custom FreeBSD service from printing stdout to the shell?

This is what I've got

#!/bin/sh
# $FreeBSD$

# PROVIDE: telegraf
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable telegrafb:
# telegraf_enable="YES"
#
# telegraf_enable (bool): Set to YES to enable telegraf
# Default: NO
# telegraf_conf (str): telegraf configuration file
# Default: ${PREFIX}/etc/telegraf.conf
# telegraf_flags (str): Extra flags passed to telegraf

. /etc/rc.subr

name="telegraf"
rcvar=telegraf_enable


load_rc_config "${name}"
: ${telegraf_enable:="YES"}
: ${telegraf_flags:="-quiet"}
: ${telegraf_conf:="/usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.conf"}

pidfile="/var/run/${name}.pid"
command=/usr/sbin/daemon
command_args="-crP ${pidfile} /usr/local/telegraf-1.23.4_freebsd_amd64/${name} ${telegraf_flags} -config=${telegraf_conf} >> /var/log/telegraf.log 2>&1"
export TELEGRAF_CONFIG_PATH="/usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.conf"



start_precmd="${name}_prestart"
start_cmd="${name}_start"

telegraf_prestart() {
# Have to empty rc_flags so they don't get passed to daemon(8)
  touch $pidfile
  rc_flags=""
}

telegraf_start() {
  ${command} ${command_args} ${rc_arg}
}

run_rc_command "$1"

This is what it does. When I crtl-c to get my shell back, the service dies.

freenas% sudo service telegraf start
2022-09-10T06:51:27Z I! Using config file: /etc/telegraf/telegraf.conf
2022-09-10T06:51:27Z I! Starting Telegraf 1.23.4
2022-09-10T06:51:27Z I! Loaded inputs: cpu disk diskio kernel mem processes swap system
2022-09-10T06:51:27Z I! Loaded aggregators: 
2022-09-10T06:51:27Z I! Loaded processors: 
2022-09-10T06:51:27Z I! Loaded outputs: prometheus_client
2022-09-10T06:51:27Z I! Tags enabled: host=freenas.local
2022-09-10T06:51:27Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"freenas.local", Flush Interval:10s
2022-09-10T06:51:27Z I! [outputs.prometheus_client] Listening on http://[::]:9100/metrics
^C2022-09-10T06:51:28Z I! [agent] Hang on, flushing any cached metrics before shutdown
2022-09-10T06:51:28Z I! [agent] Stopping running outputs

I'm sure this is probably something stupid, so I appreciate the help in advance.

CodePudding user response:

In this block of code:

telegraf_start() {
  ${command} ${command_args} ${rc_arg}
}

you should change it to become:

telegraf_start() {
  ${command} ${command_args} ${rc_arg} >>/path/to/log_file 2>/path/to/err_file
}

Also you can consider end this line with & to put the process in background

CodePudding user response:

Make sure you setup the symlink properly and call your init script and not the executable directly.

whoops.

All works as it should now.

  • Related