在crontab中的shell脚本

我可以像预期的那样从命令行运行下面的shell脚本:

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

但是当我把它设置在一个cron中,它不能正确执行。 有2个命令。 ssh -t [email protected]“sudo ls”和sudo rsync -avze到另一台服务器。

为什么在命令提示符下运行成功的shell脚本会失败?

 $ which sh /bin/sh 

我使用正确的环境吗?

更新:

 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 

在命令提示符下运行脚本时没有错误。

要更正sudo tty错误,您需要修改发出sudo命令的主机上的/ etc / sudoers文件。

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

更新/ etc / sudoers并插入!requiretty是最好的select。 但是,在某些情况下,您可能无法访问远程系统来启用/禁用/ etc / sudoers中的requiretty。

在这些情况下,您可以使用双tt作为解决方法。 双tt甚至可以在cron中工作。

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