Make bash script run as a service on Debian 8

by Daniel Pham
Published: Updated:

This article will guide you how to make a bash script run as a service on Debian 8. This article is quite simple, because I’ve previously written similar tutorials for CentOS 6, CentOS 7, Ubuntu 16. Debian 8 also uses systemd as Ubuntu 16, so the usage in these two operating systems is the same.

Create your bash script

First of all, of course, you need to create your bash script. This is your main program. Script that you want it to run as a service, 24/24 and real time.

I saved my example script onto gitlab, you can download it with the following command and use. This script collects server load information and writes it to a text file. Note, you can use your own script, this is just my demo script.

wget https://gitlab.com/Danny_Pham/WriteBash.com/raw/master/Utilities/04-Script%20get%20server%20load%20information.sh -O /opt/getload.sh
Make bash script run as a service on Debian 8
Download bash script getload.sh

Assign 700 permissions to the downloaded script (or your script).

chmod 700 /opt/getload.sh

Make bash script run as a service

Once you have the main script, you need to create the service file in systemd for your script. I named this file getload.service.

Create the service file for your bash script

You run the following command to download the service file I have written.

wget https://gitlab.com/Danny_Pham/WriteBash.com/raw/master/Config_files/01-getload.service -O /etc/systemd/system/getload.service
Make bash script run as a service on Debian 8
Download service file ‘getload.service’.

You can open the file /etc/systemd/system/getload.service and edit the informations as you want like: Description, …

You can see more at this link.

Create logrotate file

Okey, the last step, but always the important step, you know. Regardless of the program during the operation, there are have some log files. And these log files grow up day by day. You need to set up logrotate for it.

Or you can skip the setup >>/var/log/getload.log 2>&1 in the getload.service file above. Your script will run without generating any log files and you will not need to set up logrotate.

Run following command to download config file for logrotate.

wget https://gitlab.com/Danny_Pham/WriteBash.com/raw/master/Config_files/02-config_getload_logrotate -O /etc/logrotate.d/getload
Make bash script run as a service on Debian 8
Download config file for logrotate.

And done. Now we will check the result.

Check bash script running as a service

To enable your service after reboot, you run the command. If you create the service file as your own and name it different with getload, you have to replace the word getload in below command.

systemctl enable getload

To start/stop/check status of your service, run command below:

systemctl [start|stop|status] getload

Now, you can run below command to see the text file use to save server load informations.

tailf /tmp/writebash_demo.txt

Conclusion

Although I have written similar posts for some other operating systems, but in this article, I would like to guide you to implement as quickly as possible. By downloading the script file, the configuration file is available. You just have to edit a bit of information is done.

(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

Subscribe
Notify of
guest
0 Comments
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.

SUPPORT US

FOLLOW US

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

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” 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

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

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