4

I currently have an init script on a RHEL 6.x box that is used to start various license servers for an engineering group.

Each vendor has their own processes, env variables and other specifics to start/stop license servers, so over the years I have created a START, STOP, STAT script for each application that map the vendor specific stuff into a standard for our site. This allowed me to create the following generic init script that I could soft link to any application via a link in /etc/sysconfig/init.d:

#!/bin/bash
#
#   START/STOP/STAT license servers
#
# chkconfig: 345 95 05
# description: Startup/Shutdown license servers

#####################################
#   Who/When/Where
#
WHO=`who am i`
WHEN=`date`
WHERE=`hostname | cut -f1 -d "."`

#####################################
#   Figure out how we were called
#
APPNAME=`basename $0 | cut -f2 -d "_"` ; export APPNAME
SERVICELOG="/license/$APPNAME/logs/service.log" ; export SERVICELOG

echo "/etc/init.d/ License Script was ran at: $WHEN app was: $APPNAME command was: $1" >> $SERVICELOG

#################################################
#   Time to call App Specific Executable
#
case "$1" in
      start) su - licadmin /license/$APPNAME/scripts/START
             echo "$APPNAME Daemon Started $WHEN by $WHO on $WHERE" >> $SERVICELOG
              ;;
           stop)
              su - licadmin /license/$APPNAME/scripts/STOP
              echo "$APPNAME Daemon Stopped $WHEN by $WHO on $WHERE" >> $SERVICELOG
              ;;
         status)
              su - licadmin /license/$APPNAME/scripts/STAT
              ;;
 reload|restart)
              $0 stop
              $0 start
              echo "$APPNAME Daemon Restarted $WHEN by $WHO on $WHERE" >> $SERVICELOG
              ;;
              *)
              echo "Usage: $0 {start|stop|status|reload|restart}"
              exit 1
              ;;
esac
exit 0
##########################################################################
#EOF(lic_init)

I've looked into the documentation for systemd and I am really struggling on how to preserve my standard since a lot of what I do "seems" to be native to systemd (start/stop/stat) and I don't want to just have a new systemd service that just calls my Bash script.

Any ideas on how to handle my conversion?

1
  • bolting a sysv script into systemd is not unknown, see for example /usr/lib/systemd/system/libvirt-guests.service on Centos 7 (libvirt-client RPM)
    – thrig
    Commented Jul 18, 2018 at 16:59

1 Answer 1

5

Let's break it down into smaller parts:

  • WHO/WHEN/WHERE are handled by systemd's journal. You can query the journal for any unit, e.g. journalctl -u $APPNAME.service, so these are not necessary here and could be removed.
  • Likewise, $SERVICELOG can be replaced by the systemd journal. No more concerns about managing your own log rotation.
  • Custom "status" commands are supported directly by systemd. Instead, you can use systemctl status for a standardized status output. If you really want a custom status command, that would be implemented as a separate systemd service.
  • What's left is that you have multiple services that you want to stop and start the same way, by the same user. systemd template units are designed to solve that problem. You can create a single template unit, and then for each app, you can create an instance of that that template: systemctl start licensedapps@someapp

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .