文件存放位置
/usr/lib/systemd/system/xxx.service
[Unit] ===> 服务的说明
Description:描述服务
After:描述服务类别
[Service] ===> 服务运行参数的设置
Type=forking:是后台运行的形式
ExecStart:为服务的具体运行命令
ExecReload:为重启命令
ExecStop:为停止命令
PrivateTmp=True:表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install] ===> 服务安装的相关设置,可设置为多用户
示例一(nexus systemd)
[Unit]
Description=Nexus Server3.3.1 For Linux
After=network.target
[Service]
Type=forking
ExecStart=/data/nexus-3.3.1-01/bin/nexus start
ExecReload=/data/nexus-3.3.1-01/bin/nexus restart
ExecStop=/data/nexus-3.3.1-01/bin/nexus stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
示例二(weblogic systemd)
[Install]
WantedBy=default.target
[Unit]
Description=WebLogic Adminserver service
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/oracle/weblogic/user_projects/domains/base_domain
ExecStart=/opt/oracle/weblogic/user_projects/domains/base_domain/bin/startWebLogic.sh
ExecStop=/opt/oracle/weblogic/user_projects/domains/base_domain/bin/stopWebLogic.sh
[Install]
WantedBy=multi-user.target
示例三
[Unit]
Description=Node Exporter
After=network.target
[Service]
Restart=on-failure
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
示例三
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target