Skip to content

systemctl

Fri 18 Oct 2024  
🎉 Hi: ... 🎉

systemctl เป็นคำสั่งการตั้งค่าให้เริ่มทำงาน Process ในตอน Startup คำสั่งนี้จะใช้งานได้ตั้งแต่ Ubuntu server 20.04 ขึ้นไป

สร้างไฟล์ systemctl service

คำสั่งสร้าง systemctl service

bash
vi /etc/systemd/system/start-postfix.service
bash
# start-postfix.service
[Unit]

After=network.service

[Service]

ExecStart=/mnt/start-postfix.sh # Script path

[Install]

WantedBy=default.target

สร้าง bash script

bash
vi /mnt/start-postfix.sh
bash
#!/bin/bash

sleep 60
docker exec email-service service postfix start

เปิดใช้งาน systemctl service

เมื่อสร้างไฟล์ start-postfix.service เสร็จแล้วจะต้องเปิดใช้งานเพื่อให้สามารถทำงานได้ทุกครั้งหลังจาก Startup

bash
systemctl enable start-postfix.service

คำสั่งดูสถานะ systemctl service

จะต้องมีสถานะ Loaded: ... start-postfix.service; enabled; preset: enabled

bash
systemctl status start-postfix.service
# ○ docker-start-postfix.service
#      Loaded: loaded (/etc/systemd/system/start-postfix.service; enabled; preset: enabled)
#      Active: ...
#    Duration: ...
#     Process: 724 ExecStart=/mnt/start-postfix.sh (code=exited, status=0/SUCCESS)
#    Main PID: 724 (code=exited, status=0/SUCCESS)
#         CPU: ...

Built with: VitePress.