systemd service file – start service on boot

There’s lot of discussion going on about systemd, ignoring the fact it’s already here, so let’s face it and use it.

running a nodejs server as an init service

In this case i’d like to show how to run cryptpad (install howto in previous post) as an init process with systemd.

Create a cryptpad.service file in the systemd folder. For example /etc/systemd/syste/cryptpad.service

root@cryptpad:/etc/systemd/system# vi cryptpad.service
Description=Cryptpad Server

ExecStart=/usr/bin/node /home/cryptpad/cryptpad/server.js
 # Restart service after 10 seconds if node service crashes
# Output to syslog


Reload the systemd service files.

root@cryptpad:/etc/systemd/system# systemctl daemon-reload

Start the server now with systemctl (service still works)

root@cryptpad:/etc/systemd/system# systemctl restart cryptpad
root@cryptpad:/etc/systemd/system# systemctl status cryptpad 
* cryptpad.service - Cryptpad Server
 Loaded: loaded (/etc/systemd/system/cryptpad.service; enabled; vendor preset: enabled)
 Active: active (running) since Sun 2017-07-30 13:11:21 CEST; 16s ago
 Main PID: 341 (node)
 Tasks: 10 (limit: 4915)
 CGroup: /system.slice/cryptpad.service
 `-341 /usr/bin/node /home/cryptpad/cryptpad/server.js

Jul 30 13:11:21 cryptpad systemd[1]: Started Cryptpad Server.
Jul 30 13:11:22 cryptpad nodejs-cryptpad[341]: loading rpc module...
Jul 30 13:11:22 cryptpad nodejs-cryptpad[341]: [2017-07-30T11:11:22.105Z] server available http://[::]:3000
Jul 30 13:11:22 cryptpad nodejs-cryptpad[341]: Cryptpad is customizable, see customize.dist/ for details

To enable start at boot.

root@cryptpad:/etc/systemd/system# systemctl enable cryptpad
Created symlink /etc/systemd/system/ -> /etc/systemd/system/cryptpad.service.


Test the restart functionality:

Find out the PID of your cryptpad

root@cryptpad:/etc/systemd/system# netstat -tulpn | grep 'Active\|Proto\|node' 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
tcp6 0 0 :::3000 :::* LISTEN 463/node


root@cryptpad:/etc/systemd/system# ps -ef | grep server.js 
cryptpad 463 1 0 Jul22 ? 00:00:01 /usr/bin/node /home/cryptpad/cryptpad/server.js


Kill the process by it’s PID reported above

root@cryptpad:~# kill 463

Then wait a few seconds and the process will reappear with a new PID instead.

root@cryptpad:~# ps -ef | grep server.js 
cryptpad 510 1 0 Jul22 ? 00:00:01 /usr/bin/node /home/cryptpad/cryptpad/server.js

It should also reappear after a reboot without problems.

Comments are closed.