Make bash script run as a service in Debian 9

by Daniel Pham
Published: Updated:

This article will show you how to make a bash script run as a service in Debian 9. Debian is one of the most popular OSs in the world today.

Write your bash script

The first thing, of course, is that you need your bash script file. In fact, the script written in bash or another language doesn’t matter, it can be written in Perl, Python, and so on.

I wrote a demo script for this job, you download it from this link or you can write your own. You can use the following command.

wget -O /opt/

Then, you need to set permission 700 for this script.

chmod 700 /opt/
download script and make it run as a service in Debian 9
Download script and make it run as a service in Debian 9.

Recommended Reading: Make bash script run as a service on Ubuntu 16

Create service file for this script

The new Linux OSs are using the systemd structure, which makes it convenient for users to switch from one OS to another.

You type the command below to create the service file for the script, this file you put it in the directory /etc/systemd/system/.

nano /etc/systemd/system/getload.service

Then, you copy the content below to file getload.service.

Description=Getload service - WriteBash demo service in Debian 9.x

ExecStart=/bin/sh -c "/opt/ >>/var/log/getload.log 2>&1"


You can set up logrotate for the log file /var/log/getload.log that we have declared in the file service.

Enable and start service in Debian 9

After everything above has been completed, we will now type the following command to enable service getload, enabling the auto start service when the server reboots.

systemctl enable getload

Recommended Reading: Make bash script run as a service on Debian 8

If you encounter the following error, it may happen if you use Docker for example, or for some reason.

bash: systemctl: command not found

Type the following command to solve that error.

apt-get install systemd -y

And now, we use this command check the getload service works.

systemctl start getload && systemctl status getload


The article hopes that you can create the service file in Debian 9, which can make your bash script file run as a service.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.