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 \ -Xmx128M \ -Xms128M \ -XX:CompressedClassSpaceSize=64M \ -XX:MaxMetaspaceSize=128M \ -XX:MaxDirectMemorySize=64M \ -XX:+HeapDumpOnOutOfMemoryError \ -XX:HeapDumpPath=<微服务主目录>/jvm_dump/<联系devops获取微服务名>.heapdump \ -XX:+UseG1GC \ -XX:-OmitStackTraceInFastThrow \ -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 \ -Xmn1024M \ -Xms512M \ -XX:MetaspaceSize=128M \ -XX:InitiatingHeapOccupancyPercent=85 \ -XX:ParallelGCThreads=4 \ -XX:G1HeapRegionSize=32 \ -XX:MaxGCPauseMillis=200 \ -XX:+UseG1GC \ -XX:+HeapDumpOnOutOfMemoryError \ -XX:HeapDumpPath=/apps/server-admin/sunacwy-cockpit/upms-server/jvm_dump/upms-server-biz.heapdump \ -XX:-OmitStackTraceInFastThrow \ -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脚本一样必须手动执行