CentOS 增加自启脚本

方式一

直接修改 /etc/rc.local ,将任务加上即可。

但是不推荐这种方式 = =

方式二

通过增加一个 autostart.sh 的脚本

新增脚本

1
vim /etc/init.d/autostart.sh
1
2
3
4
5
6
7
#!/bin/sh
#chkconfig: 2345 80 90
#description: autostart my work shell

# 进入工作路径并启动需要的脚本
cd /home/work
./start.sh

#!/bin/sh ==> 告诉系统使用的 shell

#chkconfig: 2345 80 90 ==> 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90)

#description: xxx ==> 表示的是服务的描述信息

注意: 第二行( chkconfig )和第三行( description )必写,负责会出现如“服务 autostart.sh 不支持 chkconfig”这样的错误。

设置权限

1
chmod +x /etc/init.d/autostart.sh

设置开机启动

1
2
chkconfig --add autostart.sh
chkconfig autostart.sh on