How to start up Nodejs programs automatically on Linux, after boot.

Systemd.service - service unit configuration

Hello and welcome, how cool would it be to set up a service like an audio greeting that calls out the name of the logged-in user and then greets like, “Hello, good afternoon, welcome,…” immediately after system initialization? We can’t do that right now because you’ll need a program that does that before we can make it start automatically. But, going through this article would take you a step further to achieving that. I might as well write an article on that in the future. Right now we’re just going to start a simple Nodejs project using the "yarn start:prod" command with the assumption that it starts an application in a production environment as configured in the package.json of the project.

I understand you can use services like PM2 to monitor and start up your Nodejs apps. But let’s say you’re working on something straightforward like programs on a Raspberry Pi or you’re just craving a new adventure, if that's the case, kindly follow along.

For you to understand this, I am going to point out a few things you need to know relating to this topic.

Notes.

  • Init processes - These are the first processes (Parent) to run on the start-up of a Linux OS

    • Location - /etc/init.d
  • Systemd processes - These are children processes that could be orphaned and killed if a direct parent process dies.

    • Locations -

      • /lib/systemd/system - default - first time boot - (by system designers, maintainers)

      • /var/lib/systemd/system - locally installed packages (by system package manager e.g apt-get)

      • /run/systemd/system - transient files/ temp files

      • /etc/systemd/system - custom unit files

The etc directory has the highest priorities, it overwrites processes in other kernel locations. We will be focussing on Systemd processes in this location “/etc/systemd/system”. If you’re confused so far, don’t worry you can still follow along.

The first step is to get the location of your project, you can achieve this by running the command “$ pwd“ from the root of your Nodejs project, i.e. in the directory where you have your package.json, and running "$ ls" to list the content of the current directory. Now that we have the path to our project, I’ll go on to proceed to the next step.

To simplify things, I’m going to use a bash script to save all my commands, as I may need to chain commands together to achieve this. We’ll need a terminal editor like Emacs, Nano, or Vim, in this case, I’m going to use Nano as it looks like the easiest to use. So I’ll run the command below to create and open a new file

$ nano ~/start_metro.sh

And then inside that file, I’ll input

#!/bin/bash

cd /home/techtink/metro && yarn start:prod

With these commands, I’m simply telling it to first navigate to the metro directory and then execute the command “yarn start:prod”. Please make sure you enter the first line (i.e. #!/bin/bash ), It helps the OS recognize and execute the bash script.

Then I’m going to make the script executable by running.

$ chmod +x ~/start_metro.sh

The next step is to create a new file inside the Systemd directory (i.e - /etc/systemd/system), And it is going to require sudo privileges to create a file in that location.

sudo nano /etc/systemd/system/metro.service

It might request for password, you type in the password and press enter. It is going to open up a terminal code editor. Then I input this

[Unit]

Description=A simple metro application ## Project Description

After=network.target## Start condition (i.e. to start after connecting to a network)

[Service]

ExecStart=/home/techtink/start_metro.sh## Main process command, again I can get the location of the bash script using the "$ pwd" command. Also, we can check the contents of the current directory using "$ ls" In this case, the file is in /home/techtink

[Install]

WantedBy=multi-user.target## Tells systemd who'd want this running. In this case, it means multiple users

Save and exit.

We can then check the status using the command below

$ sudo systemctl status metro.service

You should get something like this

Now we start and enable using these commands

$ sudo systemctl start metro.service

$ sudo systemctl enable metro.service

Now you can check the status again using

$ sudo systemctl status metro.service

If you get an active and enabled status, it means your job is done. Now you can reboot using the command below

$ sudo reboot

Once it comes up and is connected, you can test if the application comes up automatically. Works?

I’ll admit, this is a bit more than the Windows version but it gives you the ability to customize even more. Awesome right?

Thank you for following.