How to Run a Script as a Service in Linux

Yann Mulonda
Geek Culture
Published in
3 min readNov 8, 2022

Running NodeJS app as a service

Intro to Systemd Services

Systemd is a software suite that provides an array of system components for Linux OS. Its main aim is to unify service configuration and behavior across Linux distributions; “system and service manager” are its primary components.

It is the first service to initialize the boot sequence and consistently runs with PID 1.

  • init.d system is the first system that Linux starts all the services and units expected to run on startup
  • Newer Linux systems have switched to system.d for services

Here are a few use-case examples of system.d:

# swithc to root:
sudo su -
# list all the services
systemctl --type=service
# start, stop and status of check tomcat service
systemctl status tomcat
systemctl stop tomcat
systemctl start tomcat
-----------------------# list all the services
service --status-all
# start, stop and status of check tomcat service
service tomcat status
service tomcat stop
service tomcat start

Systemd services can also run custom scripts that start on system boot and continue to run in the background. So in this article, we are going to set up a script that runs a NodeJS application in a Linux server to run as a service.

Image soruce: itsfoss.com

Running NodeJS App in the Background

So short review of context before we set up our script. Traditionally, we run a NodeJS application by using the following:

# using NPM
npm run start
#using node
node your_app.js

This way of running has two limitations:

  • can’t run outside the terminal or after you have logged out
  • you can’t run multiple applications within the same terminal session

There are process managers you can use to run one or multiple NodeJS applications in the background even after you log out of the Linux server. PM2 is one of the most recent NodeJS process managers with a built-in load balancer.

For the context of this article, I already have my NodeJS application and setup to run using PM2

This is an example of a script I’m going to use to run my NodeJS app:

Start Node.js App as a Service

To set up your script to run your NodeJS app as a service, do the following steps:

Copy or create your script in init.d directory:

sudo su -
cd /etc/init.d
cp /location_of_myAppexec /etc/init.d
# make sure your script is excutable
chmod +x myAppexec

Now copy that script to the RC file as well by creating symbolic links — in rc3.d and rc5.d:

ln -s /etc/init.d/myAppexec /etc/rc3.d/myAppexec
ln -s /etc/init.d/myAppexec /etc/rc5.d/myAppexec

Note:

In summary, rc. d stands for “run commands” at a run level which is their actual usage.

rc1.d through rc3.d Scripts to run when the system changes runlevels. Runlevel 1 is usually single-user mode, runlevel 2 is for multi-user

rc3. d is used for runlevel 3. All these directories contain executable scripts which have to be started at boot-up of Linux OS. As you see all the scripts are nothing but a soft link pointing to their original scripts in some other directory

Now, you can start, restart, stop, and check the status or log of your NodeJS app as a service by running the two following commands:

# you can run on CI Tool like Jenkins when building and deploying
# new change and assure that your applicate is restartert with the # fresh code while still running in the background
/etc/init.d/myAppexec start# or on the server, you can check various state of your app
# by simply run the following commands:
service myAppexec status
service myAppexec restart
service myAppexec log
service myAppexec stop
service myAppexec start

If you liked this article, you might also like; Cron & logrotate: How To Use Cron To Automate Tasks

Cheers!!!

--

--

Yann Mulonda
Geek Culture

Co-Founder & CIO @ITOT | DevOps | Senior Site Reliability Engineer @ICF󠁧󠁢󠁳󠁣󠁴 | "Learning is experience; everything else is just information!”