Skip to content

Instantly share code, notes, and snippets.

@devbkhadka
Forked from miromannino/service.md
Last active May 26, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbkhadka/9ed061f6c05a8bcdbae6fd471e46934e to your computer and use it in GitHub Desktop.
Save devbkhadka/9ed061f6c05a8bcdbae6fd471e46934e to your computer and use it in GitHub Desktop.
Sample /etc/init.d script

Sample service script for debian/ubuntu

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name
cp "service.sh" "/etc/init.d/$YOUR_SERVICE_NAME"
chmod +x /etc/init.d/$YOUR_SERVICE_NAME
chown root /etc/init.d/$YOUR_SERVICE_NAME
chgrp root /etc/init.d/$YOUR_SERVICE_NAME

Edit the script and replace following tokens:

  • <NAME> = $YOUR_SERVICE_NAME
  • <DESCRIPTION> = Describe your service here (be concise)
  • Feel free to modify the LSB header, I've made default choices you may not agree with
  • <COMMAND> = Command to start your server (for example /home/myuser/.dropbox-dist/dropboxd)
  • <USER> = Login of the system user the script should be run as (for example myuser)

Test the service

service $YOUR_SERVICE_NAME start
service $YOUR_SERVICE_NAME stop

Run at boot-time

sudo systemctl enable --now $YOUR_SERVICE_NAME

Uninstall

The service can uninstall itself with service $NAME uninstall. Yes, that's very easy, therefore a bit dangerous. But as it's an auto-generated script, you can bring it back very easily. I use it for tests and often install/uninstall, that's why I've put that here.

Don't want it? Remove lines 56-58 of the service's script.

Logs?

Your service will log its output to /var/log/$NAME.log. Don't forget to setup a logrotate :)

Problems

unable to execute /etc/init.d/$YOUR_SERVICE_NAME: No such file or directory.

Maybe it has been modified by a Windows's editor and the line endings could be not corrected. This can be verified with the command cat -v service.sh and verifying that there are some strange characters as ^M.

To fix that:

sed -i 's/\r//g' service.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides: <NAME>
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: <DESCRIPTION>
### END INIT INFO
NAME=<NAME>
SCRIPT=<COMMAND>
RUNAS=<USERNAME>
PIDFILE=/home/$RUNAS/run/$NAME.pid
LOGFILE=home/$RUNAS/log/$NAME.log
start() {
if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f <NAME> remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
retart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|uninstall}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment