How To Create a custom systemd Service in CentOS 7 – Linux

In this tutorial, I am going to show you how to create services in CentOS 7. Creating a service in CentOS 6 or its previous version will be different than creating a service in CentOS 7.

centos7

centos7

You can’t run service that was created in CentOS 7 in CentOS 6 because by default you need to install systemd package in CentOS 6 before running the service which is created in the CentOS 7.

Developers has made more changes in CentOS 7 when compared with the older versions of CentOS.

If you are proficient enough to work in CentOS 6 and its below version then, it will take some time to know about CentOS 7 and its features and then only you can work on it because of which it has more added features in it will make you get slow in understanding.

At first we can see how to start and stop the scripts in CentOS 7.  When compared to CentOS 6, starting and stopping scripts in CentOS is very easy, and managing the services are also very easy if you understand the core concept of the systemd service.

Read Article :  How to use NMAP in Kali Linux 2.0 Step by Step Tutorial

To list all the Service in CentOS 7:

The above command will list all the services and its state in the CentOS 7.  This command is similar to the services.msc which we are running in the Windows as GUI.

To start, stop & check status of Service in CentOS 7:

The above command is used to start and stop the services. The status will give you a detailed report of the Linux service and location of the directory from where the Linux services are loaded.

If you need to create a custom service in CentOS then you need to know these basic commands to manage the Linux services.

Read Article :  How to install CentOS 64-bit in Vmware

Create a script to run in Service:

To create a service, then you need any Linux package or script to use at the run time of the service. So i am going to create a script that will create a file inside /tmp directory.

save the above command inside the myservice.sh. After that use the below command to run script.

without running the above command, the script will not run. The above command will give the execute permission to the myservice.sh file.

How to create custom service:

To create a custom script, you need to go into the following directory and run the following commands. Here we are going to use the multi user agent folder, that will be loaded at the boot time.

Now paste the following code inside the testservice.service file.

Read Article :  How to Configure Samba in CentOS 6.4/Linux

Here, the script will be loaded after the network.target and it will be used by the multi-user.target.

Now enable the service to run at the startup using the command below.

After rebooting you can check the /tmp for the file “test-service-file”.

Now you have successfully created a custom service in the CentOS 7. Please provide your feedback and comment on the article.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

four + 14 =