Страница 1 из 1

Создание собственного демона и добавление его в systemd

Добавлено: 02 дек 2023, 14:27
ya
Вы можете создать собственный демон, которым можно будет управлять через systemd.

Например, нам нужно запускать все тот же скрипт /root/test.sh после перезагрузки системы. Начнем с создания файла нашей будущей службы:

touch /etc/systemd/system/test-script.service
chmod 664 /etc/systemd/system/test-script.service
nano /etc/systemd/system/test-script.service

Содержимое файла будет следующее:

Код: Выделить всё

[Unit]
Description=Template Settings Service
After=network.target
[Service]
Type=oneshot
User=root
ExecStart=/root/test.sh
[Install]
WantedBy=multi-user.target
Основные параметры:

User – пользователь под которым будет запускаться демон

Type=oneshot — процесс будет завершен до запуска дальнейших юнитов

Проверяем и перезапускаем:
# systemctl daemon-reload
# systemctl start test-script.service
# systemctl status test-script.service

● test-script.service - Test
Loaded: loaded (/etc/systemd/system/test-script.service; disabled; vendor preset: disabled)
Active: active (running)
Если вас устроило то, как работает сервис, добавьте его в автозагрузку:

# systemctl enable test-script.service

Created symlink from /etc/systemd/system/multi-user.target.wants/test-script.service to /etc/systemd/system/test-script.service.
Таким образом, вы можете добавить любой ваш скрипт в автозагрузку через systemd.

Re: Создание собственного демона и добавление его в systemd

Добавлено: 02 дек 2023, 15:49
ya

Re: Создание собственного демона и добавление его в systemd

Добавлено: 02 дек 2023, 15:58
ya
mkdir -p ~/.config/systemd/user

cat > ~/.config/systemd/user/publicapi.service << UNIT
[Unit]
Description=public api startup script

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/environment
WorkingDirectory=/home/techops
ExecStart=/home/techops/publicapi start
ExecStop=/home/techops/publicapi stop

[Install]
WantedBy=default.target
UNIT
Note that the WantedBy has to be default.target as there is no multi-user.target in the user context.

Now reload the configuration and enable the service. Again as user techops execute the commands.

systemctl --user daemon-reload
systemctl --user enable publicapi.service
systemctl --user start publicapi.service
In general you should place your systemd units in /etc/systemd/system/ not directly in /etc/systemd/system/multi-user.target.wants. When you execute systemctl enable publicapi.service a symbolic link will be created in etc/systemd/system/multi-user.target.wants or whatever target is specified for that unit.

As already mentioned, if the service/process itself can be run without root privileges you should consider adding User=techops to your unit file to run the process with a non-privileged user account.

Re: Создание собственного демона и добавление его в systemd

Добавлено: 02 дек 2023, 16:10
ya
cat ~/.config/systemd/user/vpnbook.service

Код: Выделить всё

[Unit]
Description=VPN Book

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/environment
WorkingDirectory=/home/gt
ExecStart=/home/gt/vpnscreen.sh
#ExecReload=/bin/kill -INT $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
#ExecStop=/home/techops/publicapi stop

[Install]
WantedBy=default.target
systemctl --user daemon-reload
systemctl --user enable vpnbook.service
systemctl --user start vpnbook.service