centos7如何添加开机启动项?

时间:2022-06-23
本文章向大家介绍centos7如何添加开机启动项?,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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