有时我们需要将shell脚本作为开机自启动服务,这样能避免出现问题后重新开机还需要再挨个启动服务
脚本内容如下
#!/bin/bash
#chkconfig: 2345 90 10
#description:auto_run
echo "success!" >> /root/test.txt
#chkconfig: 2345 90 10
#description:auto_run
以上两行标注蓝色的代码必须要加上,否则在执行"chkconfig --add xxx.sh"时会报如下错误:
service test.sh does not support chkconfig
chkconfig参数说明:
参数 | 说明 |
---|---|
0 | 关机 |
1 | 单用户模式 |
2 | 无网络支持的多用户模式 |
3 | 有网络支持的多用户模式 |
4 | 保留,未使用 |
5 | 有网络支持有X-Window支持的多用户模式 |
6 | 重新引导系统,即重启 |
90 | 启动优先级,优先级范围是0-100,数字越大,优先级越低。 |
10 | 启动优先级,优先级范围是0-100,数字越大,优先级越低。 |
编写完脚本后,将脚本移动到/etc/rc.d/init.d目录
mv /root/test.sh /etc/rc.d/init.d
赋予脚本执行权
chmod +x /etc/rc.d/init.d/test.sh
添加脚本到开机自启动项目中
chkconfig --add test.sh
chkconfig test.sh on
配置好这些后就可以了,可以reboot一下测试结果,我重启两次后结果如下:
cat test.txt
success!
success!