Membuat Layanan Otomatis Start Saat Server ON

From OnnoWiki
Jump to navigation Jump to search

Seringkali kita membutuhkan agar sebuah layanan/service dapat langsung jalan pada saat server boot. Untuk melakukan itu di ubuntu 20.04, kita perlu mengedit file /etc/rc.local, misalnya,

sudo nano /etc/rc.local

Isi dengan, misalnya,

  1. !/bin/bash

<your_program>.sh exit 0

Jangan lupa save file rc.local. Ganti nama <your_program>.sh dengan nama yang anda suka. Contoh isi file <your_program>.sh, misalnya untuk menjalankan kiwix secara otomatis saat server boot, maka dapat kita isi dengan perintah, misalnya,

cd /usr/local/src/kiwix-tools_linux-x86_64-3.1.2-5/ ./kiwix-serve --port=8080 wikipedia_id_top_maxi_2021-09.zim & ./kiwix-serve --port=8081 wiktionary_id_all_maxi_2021-08.zim &

Pastikan file script bisa di jalankan dengan chmod


chmod 777 <your_program>.sh chmod +x <your_program>.sh

make rc.local executable:

sudo chmod +x /etc/rc.local

edit /etc/systemd/system/rc-local.service:

sudo nano /etc/systemd/system/rc-local.service

tambahkan content berikut ke rc.local.service:

  1. SPDX-License-Identifier: LGPL-2.1+
  2. This file is part of systemd.
  3. systemd is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  1. This unit gets pulled automatically into multi-user.target by
  2. systemd-rc-local-generator if /etc/rc.local is executable.

[Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target

[Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no

[Install] WantedBy=multi-user.target

enable rc.local oleh system service:

sudo systemctl enable rc-local.service

start rc.local oleh system service:

sudo systemctl start rc-local.service

kita dapat juga men-check rc.local status:

sudo systemctl status rc-local.service