linux上添加开机自启动脚本

用来实现的服务器 linux centos 7.2

系统添加开机自启动脚本,网上查阅资料大致都有两种

1、/etc/rc.d下的rc.local中添加

2、通过chkconfig命令添加

本人在测试中先用了rc.local的方法但是都失败了,这里就不说了,有兴趣的朋友可以自行查找下。本文重点介绍第二种方法。

例如:我的目标是,在我的服务器在重启的的时候,自动的开启ngrok服务,nginx服务。
(1)先要有手动开启这两个服务的启动命令

1
2
3
4
5
6
#开启我的假网卡
ifconfig eth0:0 172.27.0.15/24 up
#启动ngrok
systemctl start ngrok.service
#启动nginx
service nginx start

如果我手动开启,需要这三个命令

(2)编写一个begin.sh文件

1
2
3
4
5
6
7
8
9
#!/bin/bash
#chkconfig: 2345 80 90
#description:开机后自动执行
#网卡
ifconfig eth0:0 172.27.0.15/24 up
#启动ngrok
systemctl start ngrok.service
#启动nginx
service nginx start

第一行 告诉系统使用的shell

第二行 告诉系统表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90)

第三行 描述信息

后面的就是你自己的执行语句了,这些语句保证在服务器的任何地方都可以执行

然后保存你的这个begin.sh文件

(3)把文件上传到/etc/rc.d/init.d这个目录下,然后执行

1
2
3
4
[root@VM_0_14_centos ~]# cd /etc/rc.d/init.d/
[root@VM_0_14_centos init.d]# chmod +x begin.sh
[root@VM_0_14_centos init.d]# chkconfig --add begin.sh
[root@VM_0_14_centos init.d]# chkconfig begin.sh on

(4)然后就是测试了,用重启命令
[root@VM_0_14_centos init.d]# reboot
当系统重新启动的时候,你的ngrok服务,nginx服务就都自动的启动了