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

centos7开机执行脚本 /etc/rc.local | Rootop 服务器运维与web架构

之前在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服务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值