1.下面是一个使用systemd写的服务的启动脚本模板:
[Unit]
Description=systemd service for <联系devops获取微服务名>
After=network.target
[Service]
Type=simple
WorkingDirectory=<微服务主目录>
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<联系devops获取微服务名>
LimitNOFILE = 65535
ExecStart= <JAVA_HOME目录>/bin/java </span>
-Xmx128M </span>
-Xms128M </span>
-XX:CompressedClassSpaceSize=64M </span>
-XX:MaxMetaspaceSize=128M </span>
-XX:MaxDirectMemorySize=64M </span>
-XX:+HeapDumpOnOutOfMemoryError </span>
-XX:HeapDumpPath=<微服务主目录>/jvm_dump/<联系devops获取微服务名>.heapdump </span>
-XX:+UseG1GC </span>
-XX:-OmitStackTraceInFastThrow </span>
-jar <微服务jar包路径> --spring.profiles.active=uat
ExecStop=/bin/kill -SIGTERM $MAINPID
[Install]
WantedBy=multi-user.target
2.下面是笔者服务的实例
[Unit]
Description=systemd service for upms-server-biz
After=network.target
[Service]
Type=simple
WorkingDirectory=/apps/server-admin/sunacwy-cockpit/upms-server/
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=upms-server-biz
LimitNOFILE = 65535
ExecStart= /usr/java/bin/java </span>
-Xmn1024M </span>
-Xms512M </span>
-XX:MetaspaceSize=128M </span>
-XX:InitiatingHeapOccupancyPercent=85 </span>
-XX:ParallelGCThreads=4 </span>
-XX:G1HeapRegionSize=32 </span>
-XX:MaxGCPauseMillis=200 </span>
-XX:+UseG1GC </span>
-XX:+HeapDumpOnOutOfMemoryError </span>
-XX:HeapDumpPath=/apps/server-admin/sunacwy-cockpit/upms-server/jvm_dump/upms-server-biz.heapdump </span>
-XX:-OmitStackTraceInFastThrow </span>
-jar /apps/server-admin/sunacwy-cockpit/upms-server/upms-server-biz.jar --spring.profiles.active=uat
ExecStop=/bin/kill -SIGTERM $MAINPID
[Install]
WantedBy=multi-user.target
3.systemd操作命令
# systemd脚本存放位置
cd /etc/systemd/system
# systemd脚本的任何改动都需要重载
systemctl daemon-reload
# 查看状态
systemctl status upms-server-biz.service
# 关闭
systemctl stop upms-server-biz.service
# 启动
systemctl start upms-server-biz.service
# 重启
systemctl restart upms-server-biz.service
4.为甚么建议使用systemd脚本而不是shell脚本
服务器重启时,可以做到自启动,无需像shell脚本一样必须手动执行