2024-05-23

Systemctl add new service.

 

Create the background service file


[Unit] Description=my-service nginx example # You may want to start after your network is ready After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/bin/env docker run --name nginx --rm -p 8080:80 docker.io/nginx:alpine Restart=Always PIDFile=/tmp/my_service_pid [Install] WantedBy=network-online.target



How to create a systemd service


# Create the service file vim /etc/systemd/system/my-service.service # (if you change an existent file, you must reload the daemon config) # Reload daemon config systemctl daemon-reload # Enable systemctl enable my-service.service # Start systemctl start my-service.service # (if you need to restart) # Restart systemctl restart my-service.service









multi-user.target.wants



/etc/systemd/system/multi-user.target.wants/

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.

# vim connection.service

and type the following (hit “i” for insert mode), save it, and exit (with “esc” and “:wq!” ) :

[Unit]
Description = making network connection up
After = network.target

[Service]
ExecStart = /root/scripts/conup.sh

[Install]
WantedBy = multi-user.target





Google Referrals