我可以使用root或我自己的用户帐户运行这个期望脚本,但是当我用cron运行它时,我总是会收到错误。操作系统是Ubuntu 8.04。
错误电子邮件如下,
Date: Thu, 26 Aug 2010 11:50:01 -0400
From: root@supa (Cron Daemon)
To: root@supa
Subject: Cron <root@supa> /home/myusername/bin/uploadmyip
myserver.com
can't read "env(USER)": no such variable
while executing
"puts $env(USER)"
(file "/home/myusername/bin/ftpxfer" line 8)
脚本'uploadmyip'如下,
#!/bin/bash
wget -O /dev/stdout http://checkip.dyndns.com/ 2> /dev/null > /tmp/myip.txt && /home/myusername/bin/ftpxfer && chmod 777 /tmp/myip.txt
期望脚本ftpxfer如下,
#!/usr/bin/env expect
if { [lrange $argv 0 0] == ""} {
}
set server myserver.com
puts $server
puts $env(USER)
system "echo "
system pwd
set timeout 5
spawn -noecho ftp $server
expect {
timeout {puts "ftp to $server timed out"; exit}
"Name ($server:$env(USER)):"
}
set timeout 300
send "username\r"
expect "Password:"
send "password\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "put /tmp/myip.txt myip.txt\r"
expect "ftp>"
send "^D\r"
“sudo crontab -l”的输出
$ sudo crontab -l
# m h dom mon dow command
50 * * * * /home/myusername/bin/uploadmyip
答案 0 :(得分:2)
答案是在您的系统上,cron没有设置USER环境变量--cron使用非常小的环境。使用LOGNAME环境变量可能会更幸运。