Source : http://www.tecmint.com/create-new-service-units-in-systemd/
In a simple way target is a collection of service units. If you want to have a look at service units running in yourgraphical.target level, type:
In a simple way target is a collection of service units. If you want to have a look at service units running in yourgraphical.target level, type:
As you can see some services are active and “running” all the time, while others run one-time and terminate (exited). If you want to check the status of a service, type:
As you can see I checked the status of
firewalld.service
(tip: you can use the auto-complete for the name of the service). It informs me that firewalld service is running all the time and it is enabled.
Don’t Miss: How to Configure FirewallD Service in CentOS 7
Enabled and disabled means the service will be permanently loaded or not, during the next boot respectively. On the other hand to start and stop a service has the limitation of the present session and it’s not permanent.
For example, if you type:
You can see that the
firewalld.service
is inactive (dead) but it is still enabled, which means that during next boot it will be loaded. So if we want a service to be loaded during boot time in the future we must enabled it. What a great conclusion! Lets create one, it’s easy.
If you go to the folder:
You can see some link files of unit services and some directories of the “wants” of a target. For example: what the multi-user target wants to be loaded when the boot procedure reaches its level, is listed in the directory with name /etc/systemd/system/multi-user.target.wants/.
As you can see it doesn’t contain only services but also other targets which are also collections of services.
Let’s make a service unit with the name connection.service.
and type the following (hit
“i”
for insert mode), save it and exit (with “esc”
and “:wq!”
) :
To explain the above: we have created a unit of service type (you can also create units of target type), we have set it to be loaded after the network.target (you can understand that the booting procedure reaches the targets with a defined order) and we want every-time the service starts to execute a bash script with the name conup.shwhich we are going to create.
The fun starts with the last part [install]. It tells that it will be wanted by “multi-user.target”. So if we enable our service a symbolic link to that service will be created inside the multi-user.target.wants folder! Got it? And if we disable it that link will be deleted. So simple.
Just enable it and check:
it informs us that the symbolic link in the multi-user.target.wants folder has been created. Check it:
As you can see “connection.service” is ready for next booting, but we must create the script file first.
Add the following line inside vim and save it:
Of course if you want your script to execute something else, you could type whatever you want instead of the second line.
For example,
that would create a file inside /tmp folder (just to check that your service is working).
We must also make the script executable:
Now we are ready. If you don’t want to wait until next boot (it’s already enabled) we can start the service for the current session typing:
Voila! My connection is up and running!
If you ‘ve chosen to write the command “touch /tmp/testbootfile” inside the script, just to check its functionality, you will see this file created inside /tmp folder.
Comentarios
Publicar un comentario