Run Command Linux as a Systemd Service
2 min read

Run Command Linux as a Systemd Service

Kadang kala beberapa command Linux yang kita install tidak tersedia secara default sebagai service. Saya ambil contoh, Node Exporter

Untuk penjelasan lebih detail tentang Node Exporter saya akan jelaskan di tulisan selanjutnya.

Jadi untuk mengatasi hal ini, kita perlu membuat service command itu sendiri.

Untuk contoh ini kita buat service yang simple dulu.

Pertama siapkan dulu command yang akan dijadikan sebagai service, lalu pindahkan command ke direktori /usr/local/bin/

$ sudo mv /home/user/node_exporter /usr/local/bin/node_exporter

Lalu buat file service nya.

$ sudo tee /etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Node Exporter service
 
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter
 
[Install]
WantedBy=multi-user.target
EOF

Setelah itu, kita perlu reload daemon service agar file service tadi dapat terupdate.

$ systemctl daemon-reload

Coba jalankan enable dan run service node_exporter.

$ systemctl start node_exporter
$ systemctl enable node_exporter
$ systemctl status node_exporter
● node_exporter.service - Node Exporter service
   Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-11-16 04:49:07 UTC; 1s ago
 Main PID: 1934 (node_exporter)
   CGroup: /system.slice/node_exporter.service
           └─1934 /usr/local/bin/node_exporter

Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=thermal_zone
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=time
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=timex
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=udp_queues
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=uname
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=vmstat
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=xfs
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:115 collector=zfs
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.043Z caller=node_exporter.go:199 msg="Listening on" address=:9100
Nov 16 04:49:07 localhost.localdomain node_exporter[1934]: level=info ts=2021-11-16T04:49:07.044Z caller=tls_config.go:191 msg="TLS is disabled." http2=false

Untuk pengetesan selanjutnya, coba reboot server dan cek kembali apakah service sudah enabled atau autorun?