0

I can run the following shell script from command prompt as expected:

/bin/sh -xv /home/shantanu/backup_transfer.sh 

But when I set it in a cron, it does not execute correctly. There are 2 commands. ssh -t [email protected] "sudo ls" and sudo rsync -avze to another server.

Why would a shell script fail in a cron when it runs successfully at command prompt?

$ which sh
/bin/sh

Am I using the correct environment?

Update:

Error for the first ssh -t command:
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: sorry, you must have a tty to run sudo

Error for the second sudo rsync command: 
sudo: sorry, you must have a tty to run sudo

No error while running the script at command prompt.

5
  • Does these commands require sudo? Did you configure with passwordless?
    – quanta
    Nov 21, 2011 at 3:07
  • Yes. it works as expected from command prompt. it is passwordless copy to remote server.
    – shantanuo
    Nov 21, 2011 at 3:13
  • 1
    1. Always use the absolute path when doing something in cron. 2. Redirect both of error and output to a log file to see what happens command >> /tmp/log 2>&1
    – quanta
    Nov 21, 2011 at 3:29
  • question updated with error out info.
    – shantanuo
    Nov 21, 2011 at 3:46
  • 1
    Use double -t or edit your /etc/sudoers file with something like this Defaults:abc !requiretty. Why didn't you search the above error in this site?
    – quanta
    Nov 21, 2011 at 3:52

2 Answers 2

2

To correct the sudo tty error you need to modify the /etc/sudoers file on the host that you are issuing the sudo command on.

#Here is an example of how to turn off the requirement of a tty for a user called "USERNAME"
Defaults:USERNAME !requiretty
1

Updating /etc/sudoers and inserting !requiretty is the best option. However, in certain cases you might not have access on the remote system to enable/disable requiretty in /etc/sudoers.

In these situations you can use a double tt as a workaround. A double tt even works in cron.

ssh -tt user@remoteserver /some/dir/remotecommand

You must log in to answer this question.

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