0%

systemd配置范例

systemd 是一个 Linux 系统基础组件的集合,提供了一个系统和服务管理器,运行为 PID 1 并负责启动其它程序。功能包括:支持并行化任务;同时采用 socket 式与 D-Bus 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 cgroups 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 支持 SysV 和 LSB 初始脚本,可以替代 sysvinit。除此之外,功能还包括日志进程、控制基础系统配置,维护登陆用户列表以及系统账户、运行时目录和设置,可以运行容器和虚拟机,可以简单的管理网络配置、网络时间同步、日志转发和名称解析等。

配置文件路径

1
2
3
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/run/zabbix/zabbix_agentd.pid
KillMode=control-group
# ExecStartPre=-/usr/bin/chmod 777 /run/zabbix/zabbix_agentd.pid
ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
ExecStartPost=-/usr/bin/chown zabbix:zabbix /run/zabbix/zabbix_agentd.pid
# ExectStopPost=-/usr/bin/chmod 777 /run/zabbix/zabbix_agentd.pid
RestartSec=10s

[Install]
WantedBy=multi-user.target

centos7 兼容6的启动脚本/etc/init.d/php-fpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#! /bin/sh
#
# chkconfig: - 84 16
# description: PHP FastCGI Process Manager
# processname: php-fpm
# config: /etc/php-fpm.conf
# config: /etc/sysconfig/php-fpm
# pidfile: /var/run/php-fpm/php-fpm.pid
#
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

# Additional environment file
if [ -f /etc/sysconfig/php-fpm ]; then
. /etc/sysconfig/php-fpm
fi

if [ "$NETWORKING" = "no" ]
then
exit 0
fi

RETVAL=0
prog="php-fpm"
pidfile=${PIDFILE-/var/run/php-fpm/php-fpm.pid}
lockfile=${LOCKFILE-/var/lock/subsys/php-fpm}

start () {
echo -n $"Starting $prog: "
dir=$(dirname ${pidfile})
[ -d $dir ] || mkdir $dir
daemon --pidfile ${pidfile} /usr/sbin/php-fpm --daemonize
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} php-fpm
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f ${lockfile} ${pidfile}
fi
}

restart () {
stop
start
}

reload () {
echo -n $"Reloading $prog: "
if ! /usr/sbin/php-fpm --test ; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $prog due to configuration syntax error"
else
killproc -p ${pidfile} php-fpm -USR2
RETVAL=$?
fi
echo
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} php-fpm
RETVAL=$?
;;
restart)
restart
;;
reload|force-reload)
reload
;;
configtest)
/usr/sbin/php-fpm --test
RETVAL=$?
;;
condrestart|try-restart)
[ -f ${lockfile} ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart|configtest}"
RETVAL=2
;;
esac

exit $RETVAL

centos7 systemd启动配置/usr/lib/systemd/system/php-fpm.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
PIDFile=/var/run/php-fpm/php-fpm.pid
EnvironmentFile=/etc/sysconfig/php-fpm
ExecStart=/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
Restart=always

[Install]
WantedBy=multi-user.target

nginx.service【官方】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

tomcat.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Apache Tomcat
Documentation=https://tomcat.apache.org/tomcat-9.0-doc/index.html
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/search/odin/webserver/tomcat/bin/catalina.map.pid
ExecStart=/search/odin/webserver/tomcat/bin/catalina.sh start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/search/odin/webserver/tomcat/bin/catalina.sh stop
PrivateTmp=true
Restart=always
RestartSec=15s
User=root
Group=root

[Install]
WantedBy=multi-user.target

相关资料

-------------End of article. I appreciate whoever read and leave commends on articles.-------------

本文标题:systemd配置范例

文章作者:冷月无声

发布时间:2018年08月10日 - 18:08

最后更新:2019年12月01日 - 21:12

原始链接:https://www.lengyuewusheng.com/systemd%E9%85%8D%E7%BD%AE%E8%8C%83%E4%BE%8B.html

许可协议:本博客所有文章除特别声明外,均采用 BY-NC-SA 3.0许可协议。转载请注明出处!