WriteBash - 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
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
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
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.
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.If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.