1.首先,需要为tomcat配置pid。
bin/catalina.sh
# Copy CATALINA_BASE from CATALINA_HOME if not already set [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
# 设置pid。一定要加在CATALINA_BASE定义后面,要不然pid会生成到/下面 CATALINA_PID="$CATALINA_BASE/tomcat.pid"
2.创建tomcat.service文件
vim /lib/systemd/system/tomcat.service
[Unit] Description=Tomcat After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking Environment="JAVA_HOME=/app/jdk1.8.0_291" PIDFile=/app/apache-tomcat-8.5.66/tomcat.pid ExecStart=/app/apache-tomcat-8.5.66/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit] 表示这是基础信息
- Description 是描述
- After 是在那个服务后面启动,一般是网络服务启动后启动
[Service] 表示这里是服务信息
- Type 是服务类型
- PIDFile 是服务的pid文件路径, 开启后,必须在tomcat的bin/catalina.sh中加入CATALINA_PID参数
- ExecStart 是启动服务的命令
- ExecReload 是重启服务的命令
- ExecStop 是停止服务的指令
[Install] 表示这是是安装相关信息
- WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
Environment="JAVA_HOME=/home/jdk/jdk1.7.0_79" 这里要配置环境变量,在/etc/profile中的配置在系统服务中不生效。
3.设置开机自启
systemctl daemon-reload #刷新配置 刚刚配置的服务需要让systemctl能识别,就必须刷新配置
systemctl enable tomcat.service
4.其它
修改tomcat.service文件后需要执行下面命令使其生效:
systemctl daemon-reload
systemctl daemon-reload
查询tomcat 进程 ps -ef | grep "tomcat"| grep -v grep
如果多个tomcat,则拷贝到不同的目录,使用不同的端口。tomcat.service文件名不同即可。
例如:tomcat1.service tomcat2.service tomcat3.service
启动nginx服务
systemctl start nginx.service
设置开机自启动
systemctl enable nginx.service
停止开机自启动
systemctl disable nginx.service
查看服务当前状态
systemctl status nginx.service
重新启动服务
systemctl restart nginx.service
查看所有已启动的服务
systemctl list-units --type=service