Starting Domoticz automatically when the system boots

There are basically two ways to do this - via init.d (which starts things serially) or via systemd (which starts things up in parallel and therefore leads to faster booting). On modern systems the systemd method is generally preferred.

Option 1: Init.d Method

To let Domoticz start automatically when the system boots (which is probably the case, as most people run it on a headless server), run the commands below, (You want to end up in /home/YOURUSERNAME/domoticz/)

cd domoticz
sudo cp domoticz.sh /etc/init.d
sudo chmod +x /etc/init.d/domoticz.sh
sudo update-rc.d domoticz.sh defaults

Edit the startup script.

sudo nano /etc/init.d/domoticz.sh

The lines you usually need to change are USERNAME, DAEMON and DAEMON_ARGS

USERNAME=yourusername
DAEMON=/home/$USERNAME/domoticz/$NAME
DAEMON_ARGS="-daemon -www 8080"
  • Change the USERNAME variable to the user you want domoticz to be run as. It's recommended to run domoticz as its own user and not as root, due to security reasons.
  • If you installed Domoticz in the USERNAME home directory /home/yourusername/domoticz you don't need to change the DAEMON variable. If you installed it in an alternate location for example /usr/local/domoticz/ you should change DAEMON to /usr/local/domoticz/$NAME
  • If you want to use another web interface port change the '8080' in: DAEMON_ARGS="-daemon www 8080" to your own port.

Note: To be able to use ports below 1024, for example the standard port for webbrowsing, 80, you need to run domoticz as the root user.

If you want to see more arguments for DAEMON_ARGS run this in the domoticz directory. This is usually not needed.

./domoticz -h

Domoticz will now start automatically when you reboot your machine.

Manually controlling the Domoticz service

This only works if you followed the above steps.

You can now start/stop/restart and check the status of domoticz with:

sudo /etc/init.d/domoticz.sh start
sudo /etc/init.d/domoticz.sh stop
sudo /etc/init.d/domoticz.sh restart
sudo /etc/init.d/domoticz.sh status

If your system supports it you can instead use:

sudo service domoticz.sh start
sudo service domoticz.sh stop
sudo service domoticz.sh restart
sudo service domoticz.sh status

Option 2: Systemd Alternative (Preferred)

Open the systemd configuration file, here you can change the port number you wish to use. To use a port below 1024 you can run as root (not reccomended) or you can un-comment one of the marked lines in the file - you should only un-comment one of the lines depending on your Ubuntu version :

vi /etc/systemd/system/domoticz.service
[Unit]
       Description=domoticz_service
[Service]
       User=domoticz
       Group=domoticz
       Wants=network-online.target
ExecStart=/home/domoticz/domoticz/domoticz -www 8080 -sslwww 443 WorkingDirectory=/home/domoticz # # Give the right to open priviliged ports. This allows you to run on a port <1024 without root permissions (user/group setting above) # # The following line is for pre-16.04 systems. # ExecStartPre=setcap 'cap_net_bind_service=+ep' /home/domoticz/domoticz/domoticz # # The below works on Ubuntu 16.04 LTS # CapabilityBoundingSet=CAP_NET_BIND_SERVICE # # The following works on Ubuntu 18.04 # AmbientCapabilities=CAP_NET_BIND_SERVICE # Restart=on-failure RestartSec=1m #StandardOutput=null [Install] WantedBy=multi-user.target

or on Raspberry Pi Stretch as root (remove '#' on User and Group lines to run as 'Pi' user):

[Unit]
      Description=domoticz_service
[Service]
      #User=pi
      #Group=users
Wants=network-online.target
ExecStartPre=/home/pi/domoticz/scripts/init2.sh ExecStart=/home/pi/domoticz/domoticz -www 8080 -sslwww 443 WorkingDirectory=/home/pi/domoticz #ExecStartPre=setcap 'cap_net_bind_service=+ep' /home/pi/domoticz/domoticz Restart=on-failure RestartSec=20 #StandardOutput=null [Install] WantedBy=multi-user.target

on Raspbian Linux 10 (buster), I had to add this just before ExecStartPre=setcap now it works. I don't know about security implications!!
The service file must add the directive "PermissionsStartOnly=true", in order to be able to execute the "ExecStartPre=/sbin/setcap 'cap_net_bind_service=+ep' ..." one!
I got this hint from the Discussion of this Wiki page

Enable the service

systemctl daemon-reload
systemctl enable domoticz.service

Start the service

systemctl start domoticz.service

Note: Use only one of these two alternatives (init.d method or systemd method) for auto-start.