"stdin: not a tty" from cronjob

I get the following mail every time I execute a specific cronjob. The script being called works fine when I call it directly and even from cron. Therefore, the message I receive is not an actual error, as the script is doing exactly what it is supposed to do.

Here is the cron.d entry:

* *     * * *     root   /bin/bash -l -c "/opt/get.sh > /tmp/file"

      

and the get.sh script itself:

#!/bin/sh

#group and url
groups="foo"

url="https://somehost.test/get.php?groups=${groups}"

# encryption
pass='bar'
method='aes-256-xts'
pass=$(echo -n $pass | xxd -ps | sed 's/[[:xdigit:]]\{2\}/&/g')

encrypted=$(wget -qO- ${url})
decoded=$(echo -n $encrypted | awk -F '#' '{print $1}')
iv=$(echo $encrypted | awk -F '#' '{print $2}' |base64 --decode | xxd -ps | sed 's/[[:xdigit:]]\{2\}/&/g')

# base64 decode input and save to file
output=$(echo -n $decoded | base64 --decode | openssl enc -${method} -d -nosalt -nopad -K ${pass} -iv ${iv})

if [ ! -z "${output}" ]; then
        echo "${output}"
else
        echo "Error while getting information"
fi

      

When I don't use the syntax bash -l

, the script hangs during the wget process. So I guess it has something to do with wget and puts the output on stdout. But I don't know how to fix it.

+3


source to share


3 answers


You actually have two questions.

  • Why is he typing stdin: is not a tty

    ?

This warning message has been printed bash -l

. Parameters -l

( --login

) are asked bash

to start a login shell, eg. the one that usually starts when the password is entered. In this case bash

, it expects its to stdin

be a real terminal (for example, the call isatty(0)

should return 1), and it is not if it is executed by cron

-hence this warning.

Another easy way to reproduce this warning, and the most common, is to run this command with ssh

:



$ ssh user@example.com 'bash -l -c "echo test"'
Password:
stdin: is not a tty
test

      

This is due to the fact that it ssh

does not allocate a terminal when called with a command as a parameter (for this, as should be used -t

) for forced allocation of terminals.

  1. Why didn't it work without -l

    ?

As @Cyrus correctly pointed out in the comments, the list of files that are bash

loaded at startup depends on the session type. For example. for input skins it loads /etc/profile

, ~/.bash_profile

, ~/.bash_login

and ~/.profile

(see. the Invocation in the manual bash(1)

), while for non-login shell is that it loads only ~/.bashrc

. It seems that you only defined your variable http_proxy

in one of the files loaded for login systems, but not in ~/.bashrc

. You moved it to ~/.wgetrc

and that's correct, but you can also define it to ~/.bashrc

and it would work.

+8


source


in your .profile, change

mesg n

      



to

if `tty -s`; then
  mesg n
fi

      

+1


source


I ended up setting the proxy config in wgetrc. Now there is no need to run the script in the login shell.

This is not a real answer to a real problem, but it solved mine.

If you encounter this problem, check if all environment variables are not set as you expect. Thanks to Cyrus for guiding me in the right direction.

0


source







All Articles