原因は、expectスクリプトの最後が、
interact
となっていたこと。
これを、
expect eof
exit
と直すことで無事動かすことが出来ました。
元のシェルスクリプト(端末で動作)
#!/bin/sh
host="user@host"
pass="password"
command="実行するコマンド"
expect -c "
set timeout 10
spawn ssh ${host} ${command}
expect \"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\n\"
expect \"${host}'s password:\"
send \"${pass}\n\"
} \"${host}'s password:\" {
send \"${pass}\n\"
}
interact
"
修正後のシェルスクリプト(crontabで動作)
#!/bin/sh
host="user@host"
pass="password"
command="実行するコマンド"
expect -c "
set timeout 10
spawn ssh ${host} ${command}
expect \"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\n\"
expect \"${host}'s password:\"
send \"${pass}\n\"
} \"${host}'s password:\" {
send \"${pass}\n\"
}
expect eof
exit
"
参考にしたページ:
crontabでexpectコマンドが動かない














