วันศุกร์ที่ 15 สิงหาคม พ.ศ. 2557

การจัดการบริการบน CentOS Linux 7.0 ด้วย Systemd

การจัดการบริการบน CentOS Linux 7.0 ด้วย Systemd



การจัดการบริการหรือ Services บนระบบปฏิบัติการ CentOS Linux รีลีสเก่าๆ (CentOS 6.x ลงมา) ในการจัดการบริการต่างๆ จะใช้รูปแบบดังนี้

การเปิดและปิดบริการ 
# service service_name start/stop/restart

การเปิดใช้งานบนบู๊ตระบบ 
# chkconfig service_name on/off
# chkconfig -list

ใน CentOS Linux 7.0 และ RHEL 7.0 ขึ้นมาจะใช้การควบคุมบริการด้วย Systemd แทน ดังนั้นการควบคุมบริการทั้งหมดจะถูกเปลี่ยนแปลงไป โดยการจัดการบริการทั้งหมดจะใช้คำสั่ง systemctl แทนคำสั่ง service และ chkconfig ที่ใช้ในรีลีสก่อนหน้า โดยในปัจจุบันมีระบบปฏิบัติการหลายตัวได้หันมาใช้งาน Systemd

ระบบปฏิบัติการที่เปลี่ยนมาใช้งาน Systemd 
- Arch Linux / October 2012
- CentOS Linux เวอร์ชั่น 7.0 ขึ้นไป / July 2014
- CoreOS / July 2013
- Debian GNU/Linux / April 2012
- Fedora เวอร์ชั่น 15 ขึ้นไป / May 2011
- Frugalware Linux เวอร์ชั่น 1.5 ขึ้นไป / August 2011
- Gentoo Linux / 2011
- Mageia เวอร์ชั่น 2.0 ขึ้นไป / May 2012
- openSUSE เวอร์ชั่น 11.4 ขึ้นไป / March 2011
- Red Hat Enterprise Linux เวอร์ชั่น 7.0 ขึ้นไป / June 2014
- Sabayon Linux เวอร์ชั่น 13.08 ขึ้นไป / August 2013
- Ubuntu เวอร์ชั่น 13.04 ขึ้นไป / April 2013

คำสั่งการใช้งานของ Systemd โดยมีรายละเอียดคำสั่งคร่าวๆ ดังนี้ 
การตรวจสอบรายชื่อบริการ (List all running services ) 
# systemctl

การเปิดบริการ (Activates a service) 
# systemctl start service_name
ตัวอย่าง เช่น
# systemctl start httpd
การปิดบริการ (Deactivates a service) 
# systemctl stop service_name
ตัวอย่าง เช่น
# systemctl stop httpd
# systemctl stop iptables
# systemctl stop ip6tables
การปรับใช้งานบริการใหม่ (Reload a service) 
* ใช้ในกรณีหลังการปรับแต่งค่าบริการหรือ config file โดยไม่ให้รบกวนบริการที่กำลังทำงานอยู่ 
# systemctl reload service_name
ตัวอย่าง เช่น
# systemctl reload httpd
การเรียกใช้งานบริการใหม่ (Restarts a service) 
# systemctl restart service_name
ตัวอย่าง เช่น
# systemctl restart httpd
การเริ่มบริการที่มีอยู่แล้วให้ทำงานใหม่ (Restarts if the service is already running) 
# systemctl condrestart service_name
ตัวอย่าง เช่น
systemctl condrestart httpd
การตรวจสอบดูสถานะของบริการ (Shows status of a service) 
# systemctl status service_name
ตัวอย่าง เช่น
# systemctl status httpd
# systemctl status iptables
การเปิดใช้งานบริการตอนบู๊ตระบบ 
การเปิดบริการตอนบู๊ตเครื่อง (Enables a service to be started on bootup)
# systemctl enable service_name
ตัวอย่าง เช่น
# systemctl enable httpd
การปิดบริการตอนบู๊ตเครื่อง (Disables a service to not start during bootup) 
# systemctl disable service_name
ตัวอย่าง เช่น
systemctl disable httpd
การเปิดบริการแบบ Manual (masked) 
# systemctl mask service_name
ตัวอย่าง เช่น
# systemctl mask dovecot
การตรวจสอบว่าบริการที่มีอยู่ถูกเปิดใช้งานอยู่หรือไม่ 
# systemctl is-enabled service_name
หรือ
# systemctl is-enabled service_name; echo $?
โดยที่
0 มีการเปิดใช้งาน
1 มีการปิดการใช้งาน
ตัวอย่าง เช่น
# systemctl is-enabled httpd
masked
# systemctl is-enabled httpd ; echo $?
masked
1
เปรียบเทียบคำสั่ง CentOS 6.x down vs CentOS 7.x up 
service service_name start = systemctl start service_name
service service_name stop = systemctl stop service_name
service service_name restart = systemctl restart service_name
service service_name reload = systemctl reload service_name
service service_name status = systemctl status service_name
chkconfig service_name on = systemctl enable service_name
chkconfig service_name off = systemctl disable service_name
chkconfig --list service_name = systemctl is-enabled service_name
chkconfig --list = systemctl list-unit-files --type=service
--
ที่มา:
http://www.freedesktop.org/wiki/Software/systemd/Incompatibilities
https://fedoraproject.org/wiki/Systemd
http://www.freedesktop.org/software/systemd/man/systemctl.html
http://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet



Write by Arnut Ruttanatirakul, Ph.D.
 See more at: http://www.sysadmin.in.th/node/309