Systemd 教程以及碰到的一些問題

參考:

阮一峯:命令篇
阮一峯:實戰篇
https://wiki.archlinux.org/index.php/Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

步驟:

 1. 寫/etc/systemd/system/xxxx.service或者/use/lib/systemd/system/xxx.service
 2. systemctl daemon-reload
 3. systemctl enable xxxxxx
 4. systemctl start xxxxxx
 5. systemctl status xxxxxx

我在配置service時碰到的問題:

  1. 有時一個命令可以啓動某個服務,但是將這個命令寫入service時並沒有啓動成功
    原因:不知道
    解決:是命令中的引號的問題
  2. 如果修改了/use/lib/systemd/system/xxx.service配置文件,需要重新加載配置文件
    systemctl daemon-reload
  3. 操作系統的原因
    原因:有的操作系統是service【ubuntu】,有的是systemctl 【centos】命令
    解決:
    service是類似於一下形式
#!/bin/bash
case "\$1" in
start)
   ${install_path}/mysqld_exporter -config.my-cnf=${install_path}/.my.cnf > /var/log/mysql_exporter.log 2>&1 &
   echo \$!>/var/run/mysql_exporter.pid
   ;;
stop)
   sudo kill \`cat /var/run/mysql_exporter.pid\`
   sudo rm -f /var/run/mysql_exporter.pid
   ;;
restart)
   \$0 stop
   \$0 start
   ;;
status)
   if [ -e /var/run/mysql_exporter.pid ]; then
      echo mysql_exporter is running, pid=\`cat /var/run/mysql_exporter.pid\`
   else
      echo mysql_exporter is NOT running
      exit 1
   fi
   ;;
*)
   echo "Usage: \$0 {start|stop|status|restart}"
esac

exit 0

systemctl使用類似的形式:

[Unit]
Description=Mysql Exporter
After=network.target
[Service]
ExecStart=${install_path}/mysqld_exporter -config.my-cnf=${install_path}/.my.cnf
[Install]
WantedBy=default.target

Shell腳本安裝成服務加入系統啓動-service

發佈了59 篇原創文章 · 獲贊 15 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章