開發與維運

CentOS7 添加開機啟動服務

文件存放位置

/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

Leave a Reply

Your email address will not be published. Required fields are marked *