1:安装expect
yum -y install expect
2:编写类似以下的shell文件 放到开机自动执行的文件夹内
/etc/rc.d/init.d
备注:
send "mima\n" ,此处的\n为输入密码后的回车,系统不同换行符也不同
source activate application 激活python的虚拟环境,切换到超级管理员用户就是为了执行此步骤
#!/bin/bash
echo "123.pyc start to run"
set timeout 5
cmd1=$(expect << EOF
spawn sudo su -
expect "*password*"
send "mima\n"
EOF)
echo "$cmd1"
source activate application
nohup python /opt/apps/123.pyc >/dev/null 2>&1 &
echo "application/123.pyc start successed"