In this post we will be using systemd to run a node application. This is helpful as it will automatically start the app when the server starts so we don’t have to manually. These steps can easily be modified to run a bash script, or any other application.
- Create systemd file
- Customize systemd file
- Enable systemd file
We’ll be creating a service for the Simple Whisper Web Interface as an example. Chang things as needed.
Create systemd file
This is super simple. We create a .service file in /lib/systemd/system. When we enable the service, it will create a symlink to this file.
sudo vim /lib/systemd/system/whisperweb.service
Customize systemd file
Change the settings as appropriate. It would be a good idea to run any service as a limited user that only has the rights needed to get the job done. Do note that you will need to have any prerequisites installed and available for that user to use. I.e. libraries installed with npm etc.
[Unit] Description=Simple Whisper Web Interface Service File After=network.target [Service] Type=simple User=whisperuser ExecStart=/usr/bin/node mainssl.js WorkingDirectory=/home/whisperuser/ Restart=on-failure [Install] WantedBy=multi-user.target
Enable systemd file
Enabling the service will create a symlink that will then run this service file on system boot.
sudo systemctl enable whisperweb.service
And now we can start the service.
sudo systemctl start whisperweb.service
We can verify that the service is running by running
sudo systemctl status whisperweb.service
The following article has some great explanations on what different options in the unit file mean and do.
https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-1/