-
Notifications
You must be signed in to change notification settings - Fork 59
Open
Labels
Description
CentOS下,开机启动脚本,可以通过配置/etc/rc.d/rc.local来设定
要配置开机自启动,先执行:
chmod +x /etc/rc.d/rc.local
1. 配置:/etc/rc.d/rc.local
[root@localhost ~]$ vi /etc/rc.d/rc.local
source /etc/bashrc
<你的初始化脚本>
如果需要以普通用户的权限运行脚本,这个时候,可以这样配置(示例),可以替换成具体的用户:
[root@localhost ~]$ vi /etc/rc.d/rc.local
source /etc/bashrc
#需要先切换到对应目录,否则脚本在写日志的情况下,会报错:permission deny
cd /home/johnnian/demo #替换成实际的脚本路径目录
su johnnian -c "/home/johnnian/demo/start.sh" #替换成实际的用户、脚本路径
2. 开机启动脚本的调试
如果发现开机启动脚本有异常,或是服务没有正常启动,可以打开调试的开关、日志:
[root@localhost ~]$ vi /etc/rc.d/rc.local
#在 #!/bin/sh 后面加上下面的配置
# send stderr from rc.local to a log file
exec 2> /tmp/rc.local.log
# send stdout to the same log file
exec 1>&2
# tell sh to display commands before execution
set -x
备注:
- 如果发现还是不会执行
/etc/rc.d/rc.local, 请确认下该脚本是否有执行的权限; - 如果没有该脚本,可以自己创建一个,或者安装ssh客户端,安装完成后会自动创建,
yum -y install openssh-clients;