Unit file หรือเรียกอีกอย่างว่า systemd เป็นตัวสำหรับจัดการตัว service ของระบบปฏิบัติการ linux ซึ่งสามารถสั่งปิดและเปิดด้วย คำสั่ง command line หรือ terminal สามารถตรวจสอบสถานะของ service ได้ unit file มีหลายประเภทด้วยกันดังนี้ .service, .socket, .device, .mount, .automount และ .path เป็นต้น ไฟล์จะเก็บไว้ในไดเรกทอรี /lib/systemd/system
เราสามารถสร้าง unit file ได้โดยมีรูปแบบการสร้างดังนี้
โค้ด: เลือกทั้งหมด
[Section]
Directive1=value
Directive2=value
. . .
โค้ด: เลือกทั้งหมด
[Unit]
Description=Dummy Service
After=multi-user.target
[email protected]
[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
โค้ด: เลือกทั้งหมด
sudo systemctl status [ ชื่อ service ]
โค้ด: เลือกทั้งหมด
service --status-all
ตัวอย่างการตรวจสอบสถานะ
สามารถ start | stop | restart ตัว service ได้ด้วยคำสั่งดังนี้
คำสั่ง start
โค้ด: เลือกทั้งหมด
sudo systemctl start [ ชื่อตัว service ]
โค้ด: เลือกทั้งหมด
sudo systemctl stop [ ชื่อตัว service ]
โค้ด: เลือกทั้งหมด
sudo systemctl restart [ ชื่อตัว service ]
อ้างอิง
https://m.do.co/c/1f99f054a010
https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux