ในบทความนี้ เราจะเรียนรู้พื้นฐานการกำหนดค่า services และ scripts ให้เริ่มต้นอัตโนมัติใน Linux CentOS/RHEL 7/8 โดยเฉพาะจะทำความรู้จักกับ systemd daemon เรียนรู้วิธีเพิ่ม/ลบ services จากการเริ่มต้น และพิจารณาวิธีการเริ่ม scripts หรือ daemons บน boot ใน Linux แบบอื่นๆ
บทความนี้มุ่งเน้นสอนให้คุณค้นหารายการ services หรือ scripts ที่เริ่มต้นอัตโนมัติใน Linux ได้อย่างรวดเร็ว เพิ่ม services หรือ scripts ของคุณเข้าไปในการเริ่มต้น หรือปิดการเริ่มต้นอัตโนมัติของบางแอปพลิเคชัน
การใช้ Systemctl จัดการ Systemd Services ใน Linux
การกระจาย Linux ยอดนิยมส่วนใหญ่ (CentOS, RHEL, Debian, Fedora, Ubuntu) ใช้ systemd startup daemon แทน init.d Systemd เป็นตัวจัดการบริการ Linux ใช้สำหรับเริ่ม daemons อื่นๆ และจัดการพวกมัน มันใช้ unit files จาก /etc/systemd/system (init.d ใช้ scripts จาก /etc/init.d/) Systemd อนุญาตให้เริ่ม services แบบขนานขนานกันขณะบูท OS
เพื่อจัดการ systemd ใช้คำสั่ง systemctl
ก่อนอื่น หลังบูทระบบ เราจะตรวจสอบรายการ units ที่มีใน systemd:
systemctl list-units

สามารถดูรายการ unit files ได้ด้วยคำสั่ง:
systemctl list-unit-files
คำสั่งนี้จะแสดง unit files ทั้งหมดที่มีอยู่
เพื่อแสดงรายการ active services และสถานะ ให้รัน:
# systemctl list-units -t service
เนื่องจากบาง units อาจกลายเป็น inactive หลัง startup สามารถดูรายการเต็มได้ด้วย option --all
# systemctl list-units --all
UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ● exim.service not-found inactive dead exim.service firewalld.service loaded active running firewalld - dynamic firewall daemon getty@tty1.service loaded active running Getty on tty1 ● iptables.service not-found inactive dead iptables.service Bring up/down networking ● NetworkManager-wait-online.service not-found inactive dead
ตามที่เห็นจากรายการ แม้ services ที่ไม่พบบนดิสก์ก็ยังแสดงอยู่
ยังสามารถใช้ flags อื่นๆ ได้ เช่น:
- --state — ใช้ตรวจสอบสถานะ daemon: Load, Active, Sub
- --type — กรอง units ตามประเภท
ตัวอย่าง:
systemctl list-units --all --state=active — แสดงเฉพาะ systemd units ที่ active
systemctl list-units --type=service — แสดงรายการ units ที่เป็น services

วิธีสร้าง Service ใน Systemd?
ในการจัดการ services systemd ใช้ syntax พิเศษ ต้องเพิ่ม .service หลังชื่อ service เช่น:
# systemctl enable nginx.service — คำสั่งเพิ่ม nginx web server เข้า startup
คำสั่งนี้จะสร้าง symbolic link ไปยังไฟล์ที่ระบุในคำสั่ง service ในไดเรกทอรี startup ของ systemd
# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
เอาต์พุทแสดงไดเรกทอรีที่สร้าง symlink ไปยัง service file
เพื่อตรวจสอบว่า service ถูกเพิ่มเข้า startup แล้วหรือยัง ให้ดู status:
systemctl status nginx.service
สังเกตบรรทัดนี้ในเอาต์พุท:
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
ค่า enabled หมายความว่า service นี้ถูกเพิ่มเข้า Linux startup แล้ว หาก service ไม่ได้เริ่มต้นอัตโนมัติ จะเห็น disabled
วิธีปิดใช้งาน Service ใน Systemd?
สามารถนำ service ออกจาก startup ได้ ทำให้ไม่เริ่มต้นตอนบูท Linux (แต่ service เองไม่ถูกลบ) เพื่อปิดการเริ่มต้นอัตโนมัติของ service ให้รัน:
# systemctl disable your_service
ตัวอย่าง ปิด nginx autostartup:
# systemctl disable nginx.service
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service
หลังจากนั้น symlink ไปยัง service file จะถูกลบออกจากไดเรกทอรี systemd สามารถตรวจสอบได้ว่า service เริ่มต้นอัตโนมัติหรือไม่:
# systemctl is-enabled nginx
วิธี Mask Units ด้วย Systemd?
ผมเคยเจอ services บางตัวที่ยังคงอยู่ใน startup หลังจาก disable แล้ว และเริ่มต้นหลัง reboot Linux เพื่อแก้ปัญหานี้ สามารถ mask service ได้:
# systemctl mask nginx.service
แล้วมันจะไม่เริ่มต้นทั้งด้วยตนเองหรือหลัง OS restart:
# systemctl mask nginx.service
Created symlink from /etc/systemd/system/nginx.service to /dev/null.
# service nginx restart
Redirecting to /bin/systemctl restart nginx.service Failed to restart nginx.service: Unit is masked.
สามารถ unmask service ได้ด้วยคำสั่ง:
# systemctl unmask nginx.service
Removed symlink /etc/systemd/system/nginx.service.
หากหลัง mask service คุณตรวจสอบ unit files จะเห็นว่า service ถูกทำเครื่องหมายเป็น masked:

รัน Script หรือ Service ด้วย Rc.local
เพื่อรัน scripts ต่างๆ เมื่อ Linux บูท rc.local มักถูกใช้
นอกจาก scripts แล้ว การใช้ rc.local ยังสามารถรัน services ได้ แม้จะเป็น services ที่เริ่มต้นด้วย systemd ผมไม่รู้ว่าทำไมต้องใช้ rc.local หากมี systemd แต่นี่คือตัวอย่าง
ก่อนอื่น /etc/rc.local ต้อง executable:
chmod +x /etc/rc.local
Rc.local ต้องเพิ่มเข้า systemd autostart:
systemctl enable rc-local
และเราสามารถเพิ่มคำสั่งเริ่ม nginx web server ลงใน rc.local:
service nginx start

แต่ผมนادرใช้ rc.local เริ่ม services บ่อยกว่านั้น rc.local ใช้เริ่ม script หรือรันคำสั่งครั้งเดียว
ตัวอย่าง ผมสร้าง script /root/test.sh ที่ทำบางอย่าง และต้องการรันทันทีหลังบูท เพิ่มบรรทัดนี้ใน rc.local:
sh /root/test.sh

ตั้งแต่ CentOS 7 นักพัฒนาระบุว่า rc.local เป็น daemon ล้าสมัยและไม่แนะนำให้ใช้เริ่ม scripts หรือ services แต่ผมยังใช้อยู่จนกว่าจะหยุดทำงาน เพราะมันง่ายมาก
วิธีสร้าง Linux Service ด้วย Systemd?
คุณสามารถสร้าง daemon ของตัวเองและจัดการผ่าน systemd
ตัวอย่าง คุณต้องการรัน script เดียวกัน (/root/test.sh) ทุกครั้งที่ระบบ reboot เริ่มต้นด้วยการสร้างไฟล์ service ใหม่:
touch /etc/systemd/system/test-script.service
chmod 664 /etc/systemd/system/test-script.service
nano /etc/systemd/system/test-script.service
เนื้อหาของไฟล์:
[Unit] Description=Template Settings Service After=network.target [Service] Type=oneshot User=root ExecStart=/root/test.sh [Install] WantedBy=multi-user.target
พารามิเตอร์หลัก:
User — บัญชีผู้ใช้ที่ daemon เริ่มต้น
Type=oneshot — systemd ควรรอให้ process จบก่อนจึงจะดำเนิน units อื่นต่อ
ตรวจสอบและ restart:
# systemctl daemon-reload
# systemctl start test-script.service
# systemctl status test-script.service
● test-script.service - Test Loaded: loaded (/etc/systemd/system/test-script.service; disabled; vendor preset: disabled) Active: active (running)
หาก service ทำงานได้ดี ให้เพิ่มเข้า systemd startup:
# systemctl enable test-script.service
Created symlink from /etc/systemd/system/multi-user.target.wants/test-script.service to /etc/systemd/system/test-script.service.
ดังนั้น คุณสามารถเพิ่ม script ใดๆ เข้า autostart และจัดการผ่าน systemd ได้
วิธีรัน Scripts ด้วย Cron?
หากต้องการรัน script หรือคำสั่งตามความถี่ ใช้ cron:
crontab -e — เปิด editor เพื่อเปลี่ยนตาราง cron task
เพิ่ม task ที่ต้องการ เช่น:
* * * * * /root/test.sh — รัน script ทุกนาที
คุณสามารถเขียน script watch-dog ที่ตรวจสอบสถานะ service และเริ่มมันถ้าไม่ทำงาน ผมใช้วิธีคล้ายกันในบางโครงการ
เพื่อแสดงรายการ tasks ทั้งหมดใน cron ให้รัน:
# crontab -l
* * * * * /root/test.sh
ค่าเวลาที่ใช้ได้สำหรับ cron tasks ตามลำดับ:
- นาที: 0-59
- ชั่วโมง: 0-59
- วันของเดือน: 1-31
- เดือน: 1-12
- วันของสัปดาห์: 0-7 (0 หรือ 7 คือวันอาทิตย์)
ใน task ของเรา script รันทุกนาที จึงมี *asterisks*
ยังสามารถวาง script ในหนึ่งในไดเรกทอรี cron:
- /cron.daily — script รันวันละครั้ง
- /cron.hourly — script รันชั่วโมงละครั้ง
- /cron.monthly — script รันเดือนละครั้ง
- /cron.weekly — script รันสัปดาห์ละครั้ง
Scripts ในไดเรกทอรีที่ระบุจะรันตามตารางอัตโนมัติ
Bash Startup Scripts: .bashrc
หากต้องการดำเนินการบางอย่างเมื่อเริ่ม SSH console สามารถเพิ่มคำสั่งหรือ script ใดๆ ลงใน .bash_profile หรือ .bashrc ทฤษฎีแล้ว เพิ่มในไฟล์ไหนก็ได้ จะรันทุกกรณี ปกติสิ่งที่ต้องการเพิ่มใน .bashrc และ .bashrc ถูกเรียกจาก .bash_profile
ผมเพิ่มคำสั่ง restart nginx web service ลงใน .bashrc:
service nginx restart

จากนั้นบันทึกไฟล์และ restart SSH session:

เห็นได้ว่าเมื่อเริ่ม terminal webserver ก็ restart ด้วย การกระทำอะไรบ้างที่ทำได้ตอนเริ่ม terminal? อาจเป็นเครื่องมือเพิ่มเติมเช่น uptime ตรวจสอบเซิร์ฟเวอร์:

หรือหากต้องการไปที่ directory เฉพาะและเริ่ม mc เมื่อรัน ssh console ให้เพิ่มใน .bashrc:
cd /var/
mc

หวังว่าบทความเกี่ยวกับการจัดการ Linux service หรือ script startup ใน Linux จะเป็นประโยชน์ (บทความนี้เขียนสำหรับ CentOS และ RHEL แต่เหมาะกับ distros อื่นๆ) ผมมั่นว่าข้อมูลนี้จะ handy สำหรับผู้เรียนรู้พื้นฐาน Linux system administration