1

I have to write a portal init.d script for a program that runs on foreground (no daemon option; no pidfile option) and logs to stdout/stderr.

The program should log into syslog service. The program should run as non root user, e.g. nobody.

The init.d file should run unter Debian and RHEL bases system like SLES 11.

I do not have the ability to install additional programs, like RHEL based procs on Debian systems.

Here are my problems

  • daemonize does not exists on SLES
  • startproc has no background option
  • start-stop-daemon has no background option and no user option.

How to route the logs syslog? Should I use >& /dev/log ? or 2>&1 | logger -t prog

A lot of examples that I found on the internet depends on /etc/init.d/functions (does not exists on SUSE), /etc/rc.status or /etc/rc.d/init.d/functions

Here is my current approch, that seems only work for SUSE:

#!/bin/bash
#
# node_exporter        This script starts and stops the node_exporter daemon
#
# chkconfig: - 85 15
# description: Node Exporter is a Prometheus exporter for hardware and OS metrics

BEGIN INIT INFO

Provides: node_exporter

Required-Start: $local_fs $network

Required-Stop: $local_fs $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: start and stop node_exporter

Description: Node Exporter is a Prometheus exporter for hardware and OS metrics

END INIT INFO

NAME=node_exporter DAEMON=/usr/local/bin/$NAME DAEMON_ARGS="--web.listen-address=:9100" PIDFILE=/var/run/$NAME.pid LOGFILE=/var/log/$NAME.log

. /lib/lsb/init-functions

start() { echo -n "Starting $NAME: " start_daemon $DAEMON $DAEMON_ARGS & echo $! > $PIDFILE echo "done." }

stop() { echo -n "Stopping $NAME: " killproc -p $PIDFILE $DAEMON echo "done." }

status() { if pidofproc -p $PIDFILE $DAEMON; then echo "$NAME not running." else echo "$NAME running." fi }

case "$1" in start) start ;; stop) stop ;; status) status ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|status|restart}" ;; esac

exit 0

Question: Is & safe to use for init.d scripts? What about LSB default? How I use them? I think, follow LSB should be cross linux distribution safe.


Since I need functions like auto-restart, should I use /etc/inittab instead a traditional init.d script?

  • um, um, do you really need to support SLES11? I mean, that's from 2009, and even its super duper long term extra extended nonplusultra support ended a year ago. If you could constrain the problem to distros that systemd, this would become quite trivial, but implementing this as an sysvinit script now means you need to then wrap that again for systemd-based versions of all your distros. – Marcus Müller Jan 19 '23 at 10:04
  • Yes. No one is able to rebuilt the legacy systems. They running some legacy software on that machine. I'm not responsible for the system security, but I now they are disconnected from internet. I'm responsible to observe CPU, Memory and Disk on that machine. I will NOT use this init.d script for systemd based system. We already have a systemd unit file. I need only help for systems they are older then my carrier. – jkroepke Jan 19 '23 at 11:48
  • What about /etc/inittab ? Should I use /etc/inittab instead init.d script? – jkroepke Jan 19 '23 at 11:55
  • Don't think so, on sysvinit, this is the right way. It's cool that you keep these systems alive! – Marcus Müller Jan 20 '23 at 08:03

0 Answers0