开发者

Why this expect script gives an error when it's run by cron?

开发者 https://www.devze.com 2023-01-13 00:42 出处:网络
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.

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] == ""} {
}

se开发者_如何转开发t 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消