Skip to main content
  1. Posts/

Automatically Restart Linux Services with Systemd

·2 mins·
linux linux
Table of Contents

Pernahkah Anda mengalami down pada service akibat Out of Memory (OOM) dan di kill oleh system? Beberapa service mungkin tidak langsung running kembali tanpa di-start secara manual sehingga dapat menyebabkan downtime yang cukup lama dan merugikan perusahaan.

Pada panduan ini, kita akan mengatur service agar dapat otomatis restart, sehingga service dapat pulih dengan cepat dan mengurangi downtime.

Edit Service
#

Anda dapat mengedit service menggunakan perintah systemctl.

systemctl edit php74-php-fpm

Kemudian tambahkan berikut.

[Service]
Restart=always
RestartSec=5s
#WatchdogSec=5
WatchdogSec dapat digunakan sebagai alternatif dari RestartSec.

Bagian Restart= dapat diisi dengan no, always, on-success, on-failure, on-abnormal, on-abort, on-watchdog.

Restart settings/Exit causes no always on-success on-failure on-abnormal on-abort on-watchdog
Clean exit code or signal X X
Unclean exit code X X
Unclean signal X X X X
Timeout X X X
Watchdog X X X X

Selain menggunakan systemctl untuk mengedit service, Anda juga bisa mengedit langsung file service yang tersimpan di /usr/lib/systemd/system/.

nano /usr/lib/systemd/system/php74-php-fpm.service

Lalu tambahkan baris berikut dibawah baris [Service].

Restart=always
RestartSec=5s
#WatchdogSec=5

Restart systemctl untuk menerapkan perubahan.

systemctl daemon-reload

Testing
#

Untuk pengetesannya, Anda perlu restart service lalu cek status service untuk mendapatkan Main PID.

systemctl restart php74-php-fpm
# systemctl status php74-php-fpm
● php74-php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php74-php-fpm.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/php74-php-fpm.service.d
           └─override.conf
   Active: active (running) since Fri 2024-08-09 00:35:17 WIB; 30min ago
 Main PID: 14128 (php-fpm)
   Status: "Processes active: 0, idle: 0, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 1 (limit: 10027)
   Memory: 18.4M
   CGroup: /system.slice/php74-php-fpm.service
           └─14128 php-fpm: master process (/etc/opt/remi/php74/php-fpm.conf)

Aug 09 00:35:17 lamp.example.host systemd[1]: Starting The PHP FastCGI Process Manager...
Aug 09 00:35:17 lamp.example.host systemd[1]: Started The PHP FastCGI Process Manager.

Selanjutnya kill Main PID service dan pantau selama 5 detik untuk memastikan service dapat otomatis restart.

kill -9 14128

Referensi:

Related

How to Block IP Targeting Specific Domains
·2 mins
linux linux
Compiling Custom Kernel Modules
·3 mins
linux linux centos
Reinstalling GRUB Bootloader
·1 min
linux linux centos
Troubleshoot grub2-install: error: unknown filesystem
·2 mins
linux linux
Setting Default Kernel in Linux
·2 mins
linux linux centos ubuntu
Use RAM to Store Temporary Data
·2 mins
linux linux