0

我可以使用 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
4

1 回答 1

2

答案是在你的系统上 cron 没有设置 USER 环境变量—— cron 使用一个非常小的环境。使用 LOGNAME 环境变量可能会更幸运。

于 2010-08-26T16:59:53.313 回答