Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have bash script

#!/bin/sh

DTFILE=/etc/daytime.addr
DTPORT=13
DAYTIME_ERROR=/tmp/dtm.err

function daytime_error(){
    if [[ -z $1 ]]
    then 
    exit 1
    fi
    if [[ -e $DAYTIME_ERROR ]]
    then
    echo "Error already reported"
    else
    logger "$1"
    touch $DAYTIME_ERROR
    fi
    exit 1
}

if [[ -s $DTFILE ]]
then
    ADDR=$(head -n1 $DTFILE)
    DAYTIME=$(telnet $ADDR $DTPORT | time_conv.awk)
    if [[ -z $DAYTIME ]]
    then
    daytime_error "Daytime client: no connection to $ADDR"
    else
    date -s "$DAYTIME"
    hwclock -w
    rm $DAYTIME_ERROR
    fi

else
    daytime_error "Daytime client: no daytime server address in file $DTFILE"
fi

and it works when called from command line, but fails when cron calls it. Specifically the line with telnet command gives zero bytes of output. Telnet has 755 mask, so every user should be able to use it. Any ideas ?

share|improve this question
    
You say this is an embedded system... could you try adding /path/to/bash /path/to/script to the cron rather than /path/to/script to humour me? Also add a full path to any non-builtin executables such as telnet? –  PriceChild Apr 21 '11 at 10:32
    
If you invoke your script with #!/bin/sh it's not really a bash script, even if /bin/sh is symlinked to bash. Bash behaves differently if invoked as "sh". –  glenn jackman Apr 21 '11 at 13:12
add comment

2 Answers

In cron, you don't have a tty session anymore, it's non interactive. I think telnet doesn't like this. You will need some other program to get the connection. A simple program to do a tcp connect and dump it's contents should be a very small program, even for embedded work.

If you're running embedded, and if you have bash, maybe your version of bash supports the /dev/tcp/host/port pseudo device. This needs to be used in redirection, not an open file. I.E. cat < /dev/tcp/host/port will work, cat /dev/tcp/host/port will give an error.

DAYTIME=$(cat < /dev/tcp/$ADDR/$DTPORT | time_conv.awk)

share|improve this answer
    
It is true that telnet complains about lack of interactive shell, but I don't have /dev/tcp. Is there any other way? –  Damir Apr 27 '11 at 10:00
    
@Damir, remember you can't do 'ls /dev/tcp', it's something in bash only. Can you do 'cat < /dev/tcp/somehost/chargen'. If not, you need something like netcat, or compile your own. –  Rich Homolka Apr 27 '11 at 17:04
add comment
DAYTIME=$(telnet $ADDR $DTPORT | time_conv.awk)

First I would say use nc not telnet - telnet attempts all sorts of interactive line negotiation (TELOPTS) and things, whereas nc just creates a raw, clean, connection.

Secondly, where is time_conv.awk? Maybe you should specify a full path to that file?

share|improve this answer
    
nc is not available, because this is used on embedded system. time_conv.awk is in /usr/bin. I've tried to put absolute paths on every call in this script, to no avail. –  Damir Apr 21 '11 at 10:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.