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
[Unit]
Description=Cryptpad Server
After=network.target

[Service]
Type=simple
User=cryptpad
WorkingDirectory=/home/cryptpad/cryptpad
ExecStart=/usr/bin/node /home/cryptpad/cryptpad/server.js
Restart=always
 # Restart service after 10 seconds if node service crashes
 RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-cryptpad
Environment=NODE_ENV=production 

[Install]
WantedBy=multi-user.target

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/readme.md for details

To enable start at boot.

root@cryptpad:/etc/systemd/system# systemctl enable cryptpad
Created symlink /etc/systemd/system/multi-user.target.wants/cryptpad.service -> /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

or

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.

https://scottlinux.com/2014/12/08/how-to-create-a-systemd-service-in-linux-centos-7/

https://github.com/xwiki-labs/cryptpad/issues/62

https://askubuntu.com/questions/676007/how-do-i-make-my-systemd-service-run-via-specific-user-and-start-on-boot

http://www.devdungeon.com/content/creating-systemd-service-files

https://www.axllent.org/docs/view/nodejs-service-with-systemd/

Comments are closed.