แนะนำการสตาร์ท สต๊อป รีสตาร์ท โปรแกรมบน CentOS 7

โปรแกรมบน CentOS ที่ควรติดตั้งใช้งานร่วมกับ VoIP Server

แนะนำการสตาร์ท สต๊อป รีสตาร์ท โปรแกรมบน CentOS 7

โพสต์โดย nuiz » 07 ส.ค. 2016 23:59

ผมขอยกตัวอย่างโปรแกรม httpd และ mysql หล่ะกันครับ คำสั่งสตาร์ท สต๊อปมันแตกต่างไปจาก CentOS 5 และ 6 ที่ผมคุ้นเคย

** แต่ที่จริงยังใช้คำสั่ง service .... start|stop ได้อยู่เหมือนเดิมครับ แต่ผมต้องปรับตัวให้เคยชินกับของใหม่ๆครับ **

1. ติดตั้ง mysql และ httpd
โค้ด: เลือกทั้งหมด
yum -y install mariadb-libs mariadb mariadb-devel httpd mod_ssl


2. จากนั้นรันคำสั่งนี้เพื่อให้ httpd และ mysql สตาร์ททุกครั้งที่เปิดเครื่อง
โค้ด: เลือกทั้งหมด
systemctl enable mariadb.service
systemctl enable httpd.service

ไม่จำเป็นต้องมี .service ตามหลังก็ได้นะครับ เช่น จะรันคำสั่งแบบนี้ก็ได้ systemctl enable mariadb และ systemctl enable httpd

3. จากนั้นสตาร์ทโปรแกรม
โค้ด: เลือกทั้งหมด
systemctl start mariadb
systemctl start httpd

หรือแบบให้มี .service ต่อท้ายก็ได้ เช่น
โค้ด: เลือกทั้งหมด
systemctl start mariadb.service
systemctl start httpd.service


4. ถ้าต้องการให้ httpd สตาร์ททีหลัง mysql เราต้องเข้าไปแก้ไขไฟล์ /usr/lib/systemd/system/httpd.service
บรรทัดนี้
After=network.target remote-fs.target nss-lookup.target

ให้ใส่ mariadb.service เพิ่มต่อท้าย แบบนี้

After=network.target remote-fs.target nss-lookup.target mariadb.service

เท่านี้ก็จะทำให้ httpd สตาร์ททีหลัง mysql แล้วครับ

แต่ท่านอาจจะปวดเฮด (เหมือนผมทีแรก) นะครับถ้าไม่ลบข้อความ network.target ออกจากบรรทัด After ไม่อย่างนั้น httpd อาจไม่สตาร์ทหลัง mariadb ก็ได้ เพราะในไฟล์ mariadb.service ก็มีข้อความ network.target อยู่ในบรรทัด After เช่นกัน ดังนั้นผมแนะนำว่าให้ลบข้อความ network.target ออกจากไฟล์ httpd.service จะดีกว่าครับ แบบนี้

After=remote-fs.target nss-lookup.target mariadb.service


เพิ่มเติมครับ...
เวลาเราติดตั้งโปรแกรมบน CentOS 7 ด้วยคำสั่ง yum เช่น yum -y install httpd เมื่อติดตั้งสำเร็จจะมีไฟล์ถูกติดตั้งในเครื่องหลายไฟล์ด้วยกันแล้วแต่โปรแกรมครับ แต่ทุกโปรแกรมจะมีไฟล์ๆหนึ่งอยู่ในโฟลเดอร์ /usr/lib/systemd/system เสมอครับ ซึ่งไฟล์นี้เอาไว้ให้เราควบคุมการสตาร์ท สต๊อป โปรแกรมนั้นครับ รวมทั้งการสั่งให้มันทำงานเป็นเซอร์วิสรันทุกครั้งที่เปิดเครื่องด้วย การสั่งงานจะทำผ่านคำสั่ง systemctl ครับ ขอยกตัวอย่างสักเล็กน้อย

สตาร์ทโปรแกรม
systemctl start httpd หรือ systemctl start httpd.service

สต๊อปโปรแกรม
systemctl stop httpd หรือ systemctl start httpd.service

ทำให้สตาร์ททุกครั้งที่เปิดเครื่อง
systemctl enable httpd หรือ systemctl enable httpd.service

ยกเลิกการสตาร์ททุกครั้งที่เปิดเครื่อง
systemctl disable httpd หรือ systemctl disable httpd.service

หน้าตาของไฟล์ /usr/lib/systemd/system/httpd.service ครับ
โค้ด: เลือกทั้งหมด
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target


บรรทัด After ให้ใส่ชื่อโปรแกรมที่จะต้องสตาร์ทก่อนที่จะสตาร์ท httpd ครับ สรุปคือ httpd จะสตาร์ทตามหลังโปรแกรมในบรรทัดนี้
บรรทัด ExecStart ใส่ชื่อคำสั่งพร้อมออปชั่น (ถ้ามี) ที่จะใช้สตาร์ทโปรแกรม

ประมาณนี้ครับ ไว้ผมมีความรู้เพิ่มเติมจะมาอัพเดทครับ
** หากมีปัญหากับอุปกรณ์ที่ซื้อมาเองหรือบริการที่ทำขึ้นมาเอง ให้โพสต์ถามในเว็บบอร์ดนี้นะครับ **
** งานเร่งด่วนติดต่อว่าจ้างที่เบอร์ 08-5161-9439 อีเมล์ iamaladin@gmail.com ไลน์ NuizVoip ครับ **
nuiz
Diamond Member
 
โพสต์: 6993
ลงทะเบียนเมื่อ: 24 มี.ค. 2010 09:33

ย้อนกลับไปยัง CentOS - The Community Enterprise Operating System

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และ บุคคลทั่วไป 2 ท่าน