1.建立service文件
以tomcat为例 , 建立tomcat.service文件
[Unit] #部分主要是对这个服务的说明,内容包括Description和After,Description 用于描述服务,After用于描述服务类别 Description=Tomcat Service After=network.target remote-fs.target nss-lookup.target [Service] #服务的一些具体运行参数的设置 #服务类型 forking代表后台运行 Type=forking #服务启动脚本 ExecStart=/home/www/apache-tomcat-8.0.33/bin/startup.sh #服务重新加载脚本 ExecReload= #服务关闭脚本 ExecStop=/home/www/apache-tomcat-8.0.33/bin/shutdown.sh #执行用户 User=www #执行用户组 Group=www [Install] #运行模式 多用户 WantedBy=multi-user.target
2.设置tomcat.service
#改变tomcat.service执行属性 chmod 754 tomcat.service #移动文件到system的service区域下 #也可以直接建文件的时候就建到该文件夹下 mv tomcat.service /usr/lib/systemd/system/tomcat.service
3.使用systemctl管理服务
#启动tomcat systemctl start tomcat.service #关闭tomcat systemctl stoptomcat.service #重启tomcat systemctl restart tomcat.service #查看启动状态 systemctl status tomcat.service #重新加载tomcat(没有配置,所以没有作用) systemctl reload tomcat.service #开机自启动tomcat systemctl enable tomcat.service #开始取消自启动 systemctl disable tomcat.service