知乎专栏 |
下面是一个启动tomcat的systemd脚本,以此脚本为例我带大家进入 systemd 的世界。
例 12.1. /usr/lib/systemd/system/tomcat.service
#################################################### # Homepage: http://netkiller.github.io # Author: netkiller<netkiller@msn.com> # Script: https://github.com/oscm/shell # Date: 2015-11-03 #################################################### [Unit] Description=Apache Tomcat Web Application Container After=network.target After=syslog.target [Service] Type=forking User=www Group=www #EnvironmentFile=/etc/sysconfig/tomcat ExecStartPre="rm -rf /srv/apache-tomcat/logs/*" ExecStart=/srv/apache-tomcat/bin/startup.sh #ExecStartPost= ExecStop=/srv/apache-tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target
脚本安装到 /usr/lib/systemd/system/tomcat.service 下面
systemctl enable tomcat systemctl start tomcat systemctl stop tomcat systemctl disable tomcat
启用脚本的时候会创建一个符号链接
[neo@netkiller ~]# ll /etc/systemd/system/multi-user.target.wants/tomcat.service lrwxrwxrwx 1 root root 38 Nov 3 04:06 /etc/systemd/system/multi-user.target.wants/tomcat.service -> /usr/lib/systemd/system/tomcat.service
Type 启动类型
User, Group 运行 ExecStart 脚本的用户,相当于 su - user -c ExecStart
Environment 环境变量,EnvironmentFile 环境变量文件
ExecStartPre 开始之前运行的脚本,ExecStart 启动脚本, ExecStartPost 启动之后运行的脚本
ExecStop 停止脚本