Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have bash script



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

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

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

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
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .