为什么这个expect脚本在由cron运行时会出错?

发布于 2024-09-16 07:02:26 字数 1312 浏览 6 评论 0原文

我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-09-23 07:02:27

答案是,在您的系统上,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文