参考:
阮一峰:命令篇
阮一峰:实战篇
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时碰到的问题:
- 有时一个命令可以启动某个服务,但是将这个命令写入service时并没有启动成功
原因:不知道
解决:是命令中的引号的问题 - 如果修改了
/use/lib/systemd/system/xxx.service
配置文件,需要重新加载配置文件
systemctl daemon-reload
- 操作系统的原因
原因:有的操作系统是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