Rootop 服务器运维与web架构

centos7开机执行脚本 /etc/rc.local

之前在centos6中添加开机启动一般都是在/etc/rc.local下添加命令或脚本
最近装了一台centos7,添加nginx开机启动命令后(源码编译),重启发现nginx没启动,再次检查/etc/rc.local文件发现需要添加权限。

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run ‘chmod +x /etc/rc.d/rc.local’ to ensure
# that this script will be executed during boot.

centos7中已经不推荐通过/etc/rc.local来实现开机运行脚本,而是通过systemd或udev规则实现。
但是对于centos7还不是很熟悉,仍然直接用rc.local实现,主要就是添加rc.local的执行权限。

[root@localhost ~]# ll /etc/rc.local #软链接,默认是有执行权限,但是rc.d/rc.local是没有执行权限的。
lrwxrwxrwx. 1 root root 13 Mar 30 16:43 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local

再次重启,脚本就可以运行了。

nginx启动另一种方法:

通过查阅资料,程序启动脚本都在/usr/lib/systemd/system下,比如nginx为nginx.service
这个脚本只有在yum安装的nginx中才会创建,源码编译的是没有的。
如果将nginx设置为开机启动则执行:
[root@localhost system]# systemctl enable nginx
ln -s ‘/usr/lib/systemd/system/nginx.service’ ‘/etc/systemd/system/multi-user.target.wants/nginx.service’

可以看到是创建软连接到/etc/systemd/system/multi-user.target.wants/下,根据这个文件手动创建源码编译的nginx启动脚本

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

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

这样就可以实现开机启动,而且可以用systemdctl start/stop/status nginx管理nginx服务。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3322.html

作者:Venus

服务器运维与性能优化

评论已关闭。