Running a PERL Script as a systemd Service

I had written a PERL server to receiving incoming requests from an external host. I needed this script to run as a service on the Linux server so wanted to add it as a systemd service.

Through some trial and error I finally got the following to work. This assumes you already have a PERL script that listens on a port for incoming requests. In the below example we will use the user ‘myserviceuser’, with the script ‘myscript.pl’ and service name ‘myscript’. You will need to change these to whatever you decide to use.

First it is good practice to run the service as a non-privileged user so create a new user with the shell /sbin/nologin.

sudo adduser myserviceuser -s /sbin/nologin

Now we want to define the service file for systemd. Run the following command to create the file:

sudo vi /usr/lib/systemd/system/myscript.service

Paste the following into vi and save the file:

[Unit]
Description=MyScript Service
After=network.target

[Service]
Type=simple
User=myserviceuser
ExecStart=/usr/bin/per /home/myserviceuser/myscript.pl
Restart=on-abort

[Install]
WantedBy=multi-user.target

Then you want to reload the systemd configurations files:

sudo systemctl daemon-reload

Now enable and start your new service:

sudo systemctl enable --now myscript 

Check the status of your new service:

systemctl status myscript

You may also like...