cd /usr/lib/systemd/system/
edit a file named simple.service with following contents:
===========================
[Unit]
Description=Simple Service
After=network.target
Description=Simple Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/test
Restart=on-abort
Type=simple
ExecStart=/usr/bin/test
Restart=on-abort
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
===========================
Here, /usr/bin/test is the example program.
Now, check the status by default
$ systemctl status simple
● simple.service - Simple Service
Loaded: loaded (/usr/lib/systemd/system/simple.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Start the service
$ sudo systemctl start simple
Now, check the status:
$ sudo systemctl status simple
output :
● simple.service - Simple Service
Loaded: loaded (/usr/lib/systemd/system/simple.service; disabled; vendor preset: disabled)
Active: active (running) since
Main PID: 23791 (test)
CGroup: /system.slice/simple.service
└─23791 /usr/bin/test
Loaded: loaded (/usr/lib/systemd/system/simple.service; disabled; vendor preset: disabled)
Active: active (running) since
Main PID: 23791 (test)
CGroup: /system.slice/simple.service
└─23791 /usr/bin/test
systemd[1]: Started Simple Service.
systemd[1]: Starting Simple Service...
systemd[1]: Starting Simple Service...
To stop the service:
$ sudo systemctl stop simple
To enable the service by default
sudo systemctl enable simple
Created symlink from /etc/systemd/system/multi-user.target.wants/simple.service to /usr/lib/systemd/system/simple.service.
$ ls -l /etc/systemd/system/multi-user.target.wants/simple.service
lrwxrwxrwx 1 root root /etc/systemd/system/multi-user.target.wants/simple.service -> /usr/lib/systemd/system/simple.service
To get more idea about different keywords used in systemd script, refer man 5 systemd.service
For example in the above one following keywords are used:
ExecStart = command executed when service is started.
After = after the specific service started.
Restart= when to restart the service.
WantedBy= which run level this is required.
No comments:
Post a Comment