How to configure OpenTSDB (or any process) as a systemd service in CentOS 7?

centos_logoCentOS 7 uses Systemd for managing services (prior to CentOS 7 it was using upstart-init.d to manage the services).

Step-1: Create CentOS 7 Service file: vim /usr/lib/systemd/system/opentsdb.service



[Unit]
Description=OpenTSDB Service
After=network.target hbase.service

[Service]
Type=forking
PrivateTmp=yes
ExecStart=/usr/share/opentsdb/etc/init.d/opentsdb start
ExecStop=/usr/share/opentsdb/etc/init.d/opentsdb stop
Restart=on-abort

[Install]
WantedBy=multi-user.target

Step-2: Let’s test the start and stop of this new OpenTSDB service



$ sudo systemctl start opentsdb
# If it starts up good, you should see the website when you goto http://<servername>:4242/
ps -leaf | grep "tsdb"
# shows you something similar then you can add it as a service to auto-start
sudo systemctl enable opentsdb

Step-3: (optional) Reboot node and make sure that the service starts up accordingly after a reboot.

If you want to learn more about the available options under systemd refer to this link here.

Some useful options to learn are: ExecStartPre=, ExecStartPost=

Some other examples for including options like the ones below can be seen here:


WorkingDirectory=/usr/share/grafana
ExecStart=/usr/sbin/grafana-server                                \
                            --config=${CONF_FILE}                 \
                            --pidfile=${PID_FILE}                 \
                            cfg:default.paths.logs=${LOG_DIR}     \
                            cfg:default.paths.data=${DATA_DIR}    \
                            cfg:default.paths.plugins=${PLUGINS_DIR}

Additionally here is one more resource.

Cheers.