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

相关文章
相关标签/搜索