views:

29

answers:

1

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
+2  A: 

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.

Bryan Oakley
You are right sir, I just tested and LOGNAME worked. Thanks.
grokus