Make bash script run as a service in CentOS 7

Share this post to your friends !

Make bash script run as a service in CentOS 7
5 (100%) 1 vote[s]

WriteBash - In this article, I will show you how to make the bash script run as a service on CentOS 7 (versions 7.x). Although CentOS 6.x is still supported by 2021 (I remember that). But the use of CentOS 7.x is getting more and more. So, I hope this article will help those who are working with CentOS 7.

Write your bash script

Please read the article “Make bash script run as service in CentOS 6” that I wrote earlier because I will reuse the content in the script in that article for this section.

You can download the script here.

The content of the script is actually quite simple, its main feature is to get information about the server load. You can see it as a program to monitor average load.

Make bash script run as a service

The next step is to initialize the init file for our new service. The difference in this step is that CentOS 7 does not use init files anymore, but rather uses systemd.

Create a service file

You create a new file with the path is /etc/systemd/system/getload.service and copy the content below into the file.

[Unit]
Description=Getload service - WriteBash demo service in CentOS 7.x
After=network.target

[Service]
Type=simple
PIDFile=/var/run/getload.pid
ExecStart=/bin/sh -c "/opt/getload.sh >>/var/log/getload.log 2>&1"
TimeoutStartSec=0
Restart=on-failure

[Install]
WantedBy=default.target

Above, you can edit the following information to match the service you want to create:

  • Description: describe your program.
  • PIDFile: the path to the PID file you want.
  • ExecStart: the command to execute the program, here we call the main script mentioned above the article.

If you want to learn more about how to create a service file, you can read more on this link.

Create logrotate file for service

And similar to the main script for the program at the beginning of this article, you can read more at the article I wrote above.

This part, for every program, needs a rotate log. So, do not ignore it because at some point your program will not be able to work because it is full of logs.

Check the result

Here you can use the systemctl command to enable the program after reboot, start or stop your service.

To enable your service after reboot, you run the command:

systemctl enable getload

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

systemctl [start|stop|status] getload

And you can see the results in the image below.

make-bash-script-run-as-service-in-centos-7 Make bash script run as a service in CentOS 7
Check getload service running in CentOS 7

If you look at the results in my article on CentOS 6, you will find it a bit different from the image above.

Conclusion

With just a few simple steps, you were able to make a bash script run as a service on CentOS 7. What are you waiting for? Write your own programs right away. Hope my article is useful for you and you also like it.

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.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.