我创建了一个 expect 脚本,当通过 cron 调用时,该脚本似乎不会运行。我在调用 cron 时包含了完整路径,如下所示。
* * * * * /usr/local/sbin/checkForRemoteTasks >/tmp/checkForRemoteTasks.output
这将调用脚本 checkForRemoteTasks 并将结果导出到 /tmp/checkForRemoteTasks.output。脚本内容如下:
#!/usr/bin/expect -f
set timeout -1
set env(TERM) vt100
if [file exists "/usr/local/sbin/remoteTasks/restartMySQL"] {
## STOP APACHE FIRST
spawn apache2ctl stop
expect "#"
spawn service mysql restart
expect "#"
spawn rm /usr/local/sbin/remoteTasks/restartMySQL
expect "#"
spawn apache2ctl start
expect "#"
}
if [file exists "/usr/local/sbin/remoteTasks/restartApache"] {
spawn apache2ctl graceful
expect "#"
spawn rm /usr/local/sbin/remoteTasks/restartApache
expect "#"
}
我的测试服务器上有一个脚本,它将文件上传到 /usr/local/sbin/remoteTasks/restartMySQL 文件。该文件包含一个字符。cron 运行的 expect 脚本(在我的生产服务器上)应检查 restartMySQL 文件,如果找到该文件,它应生成适当的命令来停止 apache、重新启动 MySQL,然后重新启动 apache。当我手动运行它时它可以工作,但通过 cron 运行时则不行。任何帮助都将不胜感激。
以下是 /tmp/checkForRemoteTasks.output 的输出:
spawn apache2ctl stop
答案1
这些命令是否真的可以交互?如果没有,只需使用exec
if {[file exists "/usr/local/sbin/remoteTasks/restartMySQL"]} {
## STOP APACHE FIRST
exec apache2ctl stop
exec service mysql restart
file delete /usr/local/sbin/remoteTasks/restartMySQL
exec apache2ctl start
}
if {[file exists "/usr/local/sbin/remoteTasks/restartApache"]} {
exec apache2ctl graceful
file delete /usr/local/sbin/remoteTasks/restartApache
}
但如果你这样做了,你最好坚持使用 shell:
#!/bin/sh
if [[ -f /usr/local/sbin/remoteTasks/restartMySQL ]]; then
## STOP APACHE FIRST
apache2ctl stop
service mysql restart
rm /usr/local/sbin/remoteTasks/restartMySQL
apache2ctl start
fi
if [[ -f /usr/local/sbin/remoteTasks/restartApache ]]; then
apache2ctl graceful
rm /usr/local/sbin/remoteTasks/restartApache
fi