为什么这个expect脚本在由cron运行时会出错?
我可以使用 root 或我自己的用户帐户运行这个 Expect 脚本,但是当我使用 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
I can run this expect script just fine using root or my own user account, however when I run it with cron, I always get an error. The OS is Ubuntu 8.04.
Error email is as follows,
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)
The script 'uploadmyip' is as follows,
#!/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
The expect script ftpxfer is as follows,
#!/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"
The output of "sudo crontab -l"
$ sudo crontab -l
# m h dom mon dow command
50 * * * * /home/myusername/bin/uploadmyip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是,在您的系统上,cron 不会设置 USER 环境变量——cron 使用非常小的环境。使用 LOGNAME 环境变量可能会更幸运。
The answer is that on your system cron doesn't set the USER environment variable -- cron uses a very minimal environment. You might have more luck using the LOGNAME environment variable.