systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以从CentOS 7开始也由systemd取代了init作为默认的系统进程管理工具
systemctl [command] [unit](配置的应用名称)
command可选项
· start:启动指定的unit systemctl start jenkins
· stop:关闭指定的unit systemctl stop jenkins
· restart:重启指定unit systemctl restart jenkins
· reload:重载指定unit systemctl reload jenkins
· enable:系统开机时自动启动指定unit,前提是配置文件中有相关配置 systemctl enable jenkins
· disable:开机时不自动运行指定unit systemctl disable jenkins
· status:查看指定unit当前运行状态 systemctl status jenkins
yum -y install gcc gcc-c++ openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl
wget http://nginx.org/download/nginx-1.15.5.tar.gz
tar zxf nginx-1.15.5.tar.gz &&
cd nginx-1.15.5
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module
make -j 4 && make install
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s reload #重启
/usr/local/nginx/sbin/nginx -s quit #关闭nginx
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl restart nginx
systemctl enable nginx
systemctl stop nginx
rpm -ivh jdk-8u231-linux-x64.rpm #rpm直接安装jdk
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_231-amd64
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
java -version #检测环境
tar -xf apache-tomcat-9.0.27
mv apache-tomcat-9.0.27 /usr/local/tomcat
启动tomcat
sh /usr/local/tomcat/bin/startup.sh #启动
sh /usr/local/tomcat/bin/shutdown.sh #关闭
vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=tomcat server
Wants=network-online.target
After=network.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/java/jdk1.8.0_231-amd64"
Environment="PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
Environment="CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl restart tomcat #启动
systemctl enable tomcat #配置自启
systemctl stop tomcat #停止服务
systemctl status tomcat #检测状态
java -jar decode.jar -Dconfig=/usr/local/lanhu/application.properties
vim demo.sh
#!/bin/bash
#
source /etc/profile
jarName="lanhu-web.jar"
workDir="/usr/local/lanhu"
start(){
cd ${workDir} && java -jar ${jarName} --spring.profiles.active=prod --server.port=9630 >uams.log 2>&1 &
}
stop(){
ps -ef | grep -qP "(?<=-jar)\s+${jarName}" && kill $(ps -ef | grep -P "(?<=-jar)\s+${jarName}" | awk '{print $2}')
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
esac
vim /usr/lib/systemd/system/lanhu.service
[Unit]
Description=uams server
Wants=network-online.target
After=network.target
[Service]
Type=forking
WorkingDirectory=/usr/local/lanhu/
ExecStart=/bin/bash uams.sh start
ExecStop=/bin/bash uams.sh stop
ExecReload=/bin/bash uams.sh restart
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl restart lanhu #启动
systemctl enable lanhu #配置自启
systemctl stop lanhu #停止服务
systemctl status lanhu #检测状态