centos7提供開啟服務啟動的方式:
1.系統服務管理命令,如果是通過yum安裝的軟件,開機啟動腳本,已經自動創建好了,直接執行如下命令
nginx.service后綴可以省略
systemctl enable nginx.service #這里nginx指的是提前配置好的開機腳本文件
systemctl start nginx.service #啟動nginx服務,也是執行一個nginx腳本文件
2.因為/usr/lib/systemd/system/這個目錄是存放啟動文件,里面一般都是 xx.service 格式
我們使用systemctl enable nginx.service 其實就是在調用/usr/lib/systemd/system/nginx.service
3.默認nginx.service腳本文件內容如下
[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
具體systemctl命令用法,請看
http://man.linuxde.net/systemctl
4.總結
1.如果你是編譯安裝的軟件,例如是redis,默認沒有redis.service 2.就進入/usr/lib/systemd/system/目錄,然后創建redis.service 普通文件,touch redis.service 3.然后寫入如下內容,路徑可能需要修改,根據自己安裝路徑修改 [Unit] Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd ExecStop=/usr/libexec/redis-shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
4.然后通過命令,添加開機啟動項
systemctl enable redis.service
systemctl start redis.service