Server Fault is a question and answer site for system and network administrators. 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 can run the following shell script from command prompt as expected:

/bin/sh -xv /home/shantanu/ 

But when I set it in a cron, it does not execute correctly. There are 2 commands. ssh -t abc@ "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

Am I using the correct environment?


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.

share|improve this question
Does these commands require sudo? Did you configure with passwordless? – quanta Nov 21 '11 at 3:07
Yes. it works as expected from command prompt. it is passwordless copy to remote server. – shantanuo Nov 21 '11 at 3:13
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 '11 at 3:29
question updated with error out info. – shantanuo Nov 21 '11 at 3:46
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 '11 at 3:52
up vote 2 down vote accepted

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
share|improve this answer

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

share|improve this answer

Your Answer


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.