Using linger with Node.js


This article explains how to use linger with a Node.js application. This combination allows your app to run persistent processes and your services to restart if your app crashes or the server reboots.

You must be on a VPS or Dedicated hosting plan to install the software in these instructions.


This article assumes you have set up a Proxy Server in the panel.

Step #1: Install linger

To install linger under your user, log in to your server via SSH and run the following command:

[server]$ loginctl enable-linger

This command will not respond with any output.

Step #2: Configure systemd user unit

This step configures Node to work with systemd user units on the server.

  1. Create the following directories:
    [server]$ mkdir -p ~/.config/systemd/user/
  2. Create a myapp.service file in the .config/systemd/user directory:
    [server]$ nano ~/.config/systemd/user/myapp.service
  3. Add the following code to this file:
    Description=Systemd instance to serve Node application
    ExecStart=/usr/bin/node /path/to/your/app/app.js
    ExecReload=/bin/kill -s HUP $MAINPID
  4. Edit the highlighted lines above as follows:
    • WorkingDirectory: The path to your website application.
    • ExecStart: The path to your node installation.
  5. Enable the user unit for your Node app:
    [server]$ systemctl --user enable myapp
  6. Start the user unit for your Node app:
    [server]$ systemctl --user start myapp
  7. Confirm it is running:
    [server]$ systemctl --user status myapp

Your app's services will now automatically restart when the server reboots or if the application crashes.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?