🐧 Как отложить запуск службы systemd

by itisgood

Отложитм запуск службы systemd при загрузке.

Проверьте службу, запуск которой вы хотите отложить при загрузке.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-12 21:23:37 UTC; 12s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 907 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2311)
     Memory: 7.2M
        CPU: 114ms
     CGroup: /system.slice/redis-server.service
             └─907 /usr/bin/redis-server 127.0.0.1:6379

Sep 12 21:23:37 bullseye systemd[1]: Starting Advanced key-value store...
Sep 12 21:23:37 bullseye systemd[1]: Started Advanced key-value store.

Создайте триггер таймера для запуска службы через 1 минуту после загрузки системы.

Обратите внимание, что таймер использует то же имя службы.

$ cat <<EOF | sudo tee /etc/systemd/system/redis-server.timer
[Unit]
Description=Timer for the redis-server service

[Timer]
OnBootSec=1min

[Install]
WantedBy=timers.target
EOF

Перезагрузите конфигурацию systemd.

$ sudo systemctl daemon-reload

Отключите службу при загрузке.

$ sudo systemctl disable redis-server
Removed /etc/systemd/system/multi-user.target.wants/redis-server.service.
Removed /etc/systemd/system/redis.service.

Включите таймер:

$ sudo systemctl enable redis-server.timer 
Created symlink /etc/systemd/system/timers.target.wants/redis-server.timer → /etc/systemd/system/redis-server.timer.

Перезапустите контейнер.

$ uptime
 21:34:09 up 0 min,  1 user,  load average: 0.00, 0.00, 0.00

По умолчанию служба будет отключена.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● redis-server.timer
       Docs: http://redis.io/documentation,
             man:redis-server(1)

Таймер сработает автоматически.

$ sudo systemctl list-timers 
NEXT                        LEFT          LAST                        PASSED     UNIT                         ACTIVATES
Sun 2021-09-12 21:34:46 UTC 30s left      n/a                         n/a        redis-server.timer           redis-server.service
Sun 2021-09-12 21:48:46 UTC 14min left    n/a                         n/a        systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2021-09-13 00:00:00 UTC 2h 25min left Sun 2021-09-12 11:57:28 UTC 9h ago     logrotate.timer              logrotate.service
Mon 2021-09-13 00:00:00 UTC 2h 25min left Sun 2021-09-12 11:57:28 UTC 9h ago     man-db.timer                 man-db.service
Mon 2021-09-13 00:02:04 UTC 2h 27min left Fri 2021-09-10 09:41:16 UTC 2 days ago fstrim.timer                 fstrim.service
Mon 2021-09-13 00:28:54 UTC 2h 54min left Sun 2021-09-12 15:20:37 UTC 6h ago     apt-daily.timer              apt-daily.service
Mon 2021-09-13 06:22:58 UTC 8h left       Sun 2021-09-12 12:00:52 UTC 9h ago     apt-daily-upgrade.timer      apt-daily-upgrade.service
Sun 2021-09-19 03:10:52 UTC 6 days left   Sun 2021-09-12 11:57:36 UTC 9h ago     e2scrub_all.timer            e2scrub_all.service

8 timers listed.
Pass --all to see loaded but inactive timers, too.

Подождите оставшееся время и проверьте сервис.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-12 21:34:46 UTC; 5s ago
TriggeredBy: ● redis-server.timer
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 562 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2311)
     Memory: 10.1M
        CPU: 104ms
     CGroup: /system.slice/redis-server.service
             └─562 /usr/bin/redis-server 127.0.0.1:6379

Sep 12 21:34:46 bullseye systemd[1]: Starting Advanced key-value store...
Sep 12 21:34:46 bullseye systemd[1]: Started Advanced key-value store.

см. также:

 

You may also like

Leave a Comment