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.
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.
To list all the Service in CentOS 7:
# systemctl --type=service
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:
# systemctl start firewalld.service
# systemctl stop firewalld.service
# systemctl status firewalld.service
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.
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.
# cd /
# mkdir scripts
# cd scripts
# vim myservice.sh
save the above command inside the myservice.sh. After that use the below command to run script.
# chmod 755 myservice.sh
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.
# cd /etc/systemd/system/multi-user.target.wants/
# vim testservice.service
Now paste the following code inside the testservice.service file.
Description = Test Service by techken.in
After = network.target
ExecStart = /root/scripts/myservice.sh
WantedBy = multi-user.target
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.
# systemctl enable testservice.service
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.