Skip to content

Instantly share code, notes, and snippets.

@annesteenbeek
Last active April 11, 2017 00:04
Show Gist options
  • Save annesteenbeek/757b67163baea35735094fe93da14017 to your computer and use it in GitHub Desktop.
Save annesteenbeek/757b67163baea35735094fe93da14017 to your computer and use it in GitHub Desktop.

Ubuntu Demon Creator

An automated way of creating services/demons for ubuntu 16.04. source: https://github.com/x13machine/ubuntu-demon-creator

I made this script for other automated bash scripts and to fix some bugs in this https://gist.github.com/naholyr/4275302.

Example

curl https://gist.githubusercontent.com/annesteenbeek/757b67163baea35735094fe93da14017/raw/df7a3a84eef139d445a99bd2cf7382797dc739c8/create-deamon.sh | sudo name="web" username="web" command="/home/web/start.sh" bash

Logs

/var/log/<name>.log

Removal

service uninstall <name>

Parameters

  • name: Name of the demon.
  • username: The user the demon will run as.
  • command: The command to run the demon.
  • description (optional): The description of the demon.
#!/bin/bash
#based off: https://gist.github.com/naholyr/4275302
set -e
template=$'#!/bin/sh\n### BEGIN INIT INFO\n# Provides: <NAME>\n# Required-Start: $local_fs $network $named $time $syslog\n# Required-Stop: $local_fs $network $named $time $syslog\n# Default-Start: 2 3 4 5\n# Default-Stop: 0 1 6\n# Description: <DESCRIPTION>\n### END INIT INFO\n\nSCRIPT=<COMMAND>\nRUNAS=<USERNAME>\n\nPIDFILE=/var/run/<NAME>.pid\nLOGFILE=/var/log/<NAME>.log\n\nstart() {\n if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then\n echo \'Service already running\' >&2\n return 1\n fi\n echo \'Starting service…\' >&2\n local CMD="$SCRIPT &> \\"$LOGFILE\\" & echo \\$!"\n su -c "$CMD" $RUNAS > "$PIDFILE"\n echo \'Service started\' >&2\n}\n\nstop() {\n if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then\n echo \'Service not running\' >&2\n return 1\n fi\n echo \'Stopping service…\' >&2\n kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"\n echo \'Service stopped\' >&2\n}\n\nuninstall() {\n echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "\n local SURE\n read SURE\n if [ "$SURE" = "yes" ]; then\n stop\n rm -f "$PIDFILE"\n echo "Notice: log file is not be removed: \'$LOGFILE\'" >&2\n update-rc.d -f <NAME> remove\n rm -fv "$0"\n fi\n}\n\ncase "$1" in\n start)\n start\n ;;\n stop)\n stop\n ;;\n uninstall)\n uninstall\n ;;\n restart)\n stop\n start\n ;;\n *)\n echo "Usage: $0 {start|stop|restart|uninstall}"\nesac'
#verify
if [ -z ${name+x} ] || [ -z ${username+x} ] || [ -z ${command+x} ]; then
echo "Error: All parameters need to be set"
exit 1
fi
if [ -f "/etc/init.d/$name" ]; then
echo "Error: Service '$name' already exists"
exit 1
fi
if ! id -u "$username" &> /dev/null; then
echo "Error: User '$username' not found"
exit 1
fi
#fill in template
template="${template//<NAME>/$name}"
template="${template//<USERNAME>/$username}"
template="${template//<DESCRIPTION>/${description:-foo}}"
template="${template//<COMMAND>/$(printf %q "$command")}"
#install
echo -e "$template" > "/etc/init.d/$name"
chmod +x "/etc/init.d/$name"
touch "/var/log/$name.log"
chown "$username" "/var/log/$name.log"
update-rc.d "$name" defaults
service "$name" start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment