centos7如何添加開機啓動項?

centos7提供開啓服務啓動的方式:

1.系統服務管理命令,若是是經過yum安裝的軟件,開機啓動腳本,已經自動建立好了,直接執行以下命令linux

nginx.service後綴能夠省略nginx

systemctl enable nginx.service #這裏nginx指的是提早配置好的開機腳本文件redis

systemctl start nginx.service #啓動nginx服務,也是執行一個nginx腳本文件centos


2.由於/usr/lib/systemd/system/這個目錄是存放啓動文件,裏面通常都是 xx.service 格式app

咱們使用systemctl enable nginx.service 其實就是在調用/usr/lib/systemd/system/nginx.servicecentos7


3.默認nginx.service腳本文件內容以下spa

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target.net

[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=truecode

[Install]
WantedBy=multi-user.targetserver

 

 

具體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.servicesystemctl start redis.service

相關文章
相關標籤/搜索