Skip to content

Instantly share code, notes, and snippets.

@wreckoner
Last active September 29, 2016 15:34
Show Gist options
  • Save wreckoner/310f01a588916dde0d4a07f6d22fe4f6 to your computer and use it in GitHub Desktop.
Save wreckoner/310f01a588916dde0d4a07f6d22fe4f6 to your computer and use it in GitHub Desktop.
Create system user in Ubuntu and run a program as a service.
# This gist shows how to create a system user with least required privileges to run soffice as a service.
# Can be used as a template to run any program as a service in debian based distros.
# Create system user with its own group, no shell, password disabled and login disabled.
# In this case the user needs a home directory which is going to be used by the program to write configurations to.
# So we specify /srv/soffice as home directory
sudo adduser --system --shell /bin/false --home /srv/soffice --disabled-password --disabled-login --group soffice
# Make a copy of the file /etc/init.d/skeleton nd rename it to /etc/init.d/soffice
sudo mv /etc/init.d/skeleton /etc/init.d/soffice
# Edit the file, so that the file looks like the following
#!/bin/sh
### BEGIN INIT INFO
# Provides: libreoffice headless server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: LibreOffice Headless Server
# Description: This file starts and stops the LibreOffice headless server, which listens on localhost (127.0.0.1) on port 8100
#
### END INIT INFO
# Author: Dibyendu Das
DESC="This file starts and stops the LibreOffice headless server, which listens on localhost (127.0.0.1) on port 8100"
case "$1" in
start)
su soffice -s /bin/bash -c "soffice --nologo --headless --nofirststartwizard --accept='socket,host=127.0.0.1,port=8100,tcpNoDelay=1;urp' &"
echo "Started soffice headless server listening on port 8100"
;;
stop)
kill $(pidof soffice.bin | awk '{print $2}')
sleep 5
echo "Stopped soffice headless server"
;;
restart)
stop
sleep 10
start
;;
*)
echo "Usage: tomcat {start|stop|restart}" >&2
exit 3
;;
esac
# Make the script executable:
sudo chmod a+x /etc/init.d/soffice
# Test the script by running it from the command line.
sudo /etc/init.d/soffice start
sudo /etc/init.d/soffice stop
# If all goes well then register the script as an init script:
sudo update-rc.d soffice defaults
# References
# [1] https://mobiarch.wordpress.com/2014/05/16/creating-an-init-script-in-ubuntu-14-04/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment