linux开机自启动的方式跟window相似,也是把启动程序放在一个公共位置,随着开机自启动。
在CentOS 之前版本 ,把启动脚本放在/etc/rc.d/rc.local目录即可。但CentOS 7,/etc/rc.d/rc.local
文件的权限被降低了,开机的时候执行自己的脚本是不能起动一些服务的。
查看下该文件的权限,就会知道,该文件没有执行权限。
[root@localhost ~]# cd /etc/rc.d/ [root@localhost rc.d]# ll 总用量 4 drwxr-xr-x. 2
root root70 2月 27 23:30 init.d drwxr-xr-x. 2 root root 45 2月 27 23:30 rc0.d
drwxr-xr-x. 2 root root 45 2月 27 23:30 rc1.d drwxr-xr-x. 2 root root 45 2月 27 23
:30 rc2.d drwxr-xr-x. 2 root root 45 2月 27 23:30 rc3.d drwxr-xr-x. 2 root root
45 2月 27 23:30 rc4.d drwxr-xr-x. 2 root root 45 2月 27 23:30 rc5.d drwxr-xr-x. 2
root root45 2月 27 23:30 rc6.d -rw-r--r--. 1 root root 473 8月 5 2017 rc.local
而且查看rc.local文件,里面的注释也挺人性化,告诉你要一定要执行chmod +x /etc/rc.d/rc.local
[root@localhost rc.d]# cat rc.local #!/bin/bash # 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 contrast 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. touch /var/lock/subsys/local [root@localhost rc.d]#
实验
比如说:开机自动启动nginx服务(最近刚在虚拟机安装了一个nginx,拿来小试牛刀下)
先检查我当前nginx是未启动的
[root@localhost ~]# ps -ef|grep nginx root 1238 1126 0 09:44 pts/0 00:00:00
grep --color=auto nginx [root@localhost ~]#
一、新建执行脚本hbk.sh
脚本的位置在/root/hbk目录下
[root@localhost hbk]# pwd /root/hbk [root@localhost hbk]# ll 总用量 35820
-rw-r--r--.1 root root 40 4月 13 09:47 hbk.sh drwxr-xr-x. 3 root root 53 4月 12 17
:33 nginx drwxr-xr-x. 11 root root 151 4月 13 08:51 nginx2 drwxr-xr-x. 10 502
games4096 11月 2 02:52 zookeeper-3.4.11 -rw-r--r--. 1 root root 36668066 11月 9 02
:24 zookeeper-3.4.11.tar.gz [root@localhost hbk]# cat hbk.sh #!/bin/bash
/root/hbk/nginx2/sbin/nginx [root@localhost hbk]#
可以从上面看到hbk.sh没有执行权限。
二、使自启动程序拥有执行权限
[root@localhost hbk]# chmod +x hbk.sh [root@localhost hbk]# ll 总用量 35820
-rwxr-xr-x. 1 root root 40 4月 13 09:47 hbk.sh drwxr-xr-x. 3 root root 53 4月 12
17:33 nginx drwxr-xr-x. 11 root root 151 4月 13 08:51 nginx2 drwxr-xr-x. 10 502
games 4096 11月 2 02:52 zookeeper-3.4.11 -rw-r--r--. 1 root root 36668066 11月 9
02:24 zookeeper-3.4.11.tar.gz [root@localhost hbk]#
三、在/etc/rc.d/rc.local中加入执行脚本命令,并设置执行权限。
在/etc/rc.d/rc.local文件末尾追加/root/hbk/hbk.sh
[root@localhost hbk]# vi /etc/rc.d/rc.local [root@localhost hbk]# chmod +x
/etc/rc.d/rc.local [root@localhost hbk]# ll /etc/rc.d/rc.local -rwxr-xr-x. 1
root root492 4月 13 09:55 /etc/rc.d/rc.local [root@localhost hbk]#
简单的三步操作之后,重启下linux系统,验证下nginx服务已经开机启动了。
[root@localhost hbk]# reboot Connecting to 192.168.254.128:22... Connection
established.To escape to local shell, press 'Ctrl+Alt+]'. WARNING! The remote
SSH server rejected X11 forwarding request. Last login: Fri Apr13 09:26:31 2018
from192.168.254.1 [root@localhost ~]# ps -ef|grep nginx root 913 1 0 09:57 ? 00:
00:00 nginx: master process /root/hbk/nginx2/sbin/nginx nobody 915 913 0 09:57 ?
00:00:00 nginx: worker process nobody 916 913 0 09:57 ? 00:00:00 nginx: worker
process nobody 917 913 0 09:57 ? 00:00:00 nginx: worker process nobody 918 913 0
09:57 ? 00:00:00 nginx: worker process root 1050 1034 0 09:59 pts/0 00:00:00
grep--color=auto nginx [root@localhost ~]#
热门工具 换一换