LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   SSH Cron (https://www.linuxquestions.org/questions/linux-software-2/ssh-cron-601769/)

just1812 11-22-2007 07:33 PM

SSH Cron
 
Hi

I have created an ssh script with following ssh command in it.

ssh -i /home/user/.ssh/sshkey -l username server cat file.txt > /home/user/Desktop/file2.txt

This command works when I run it interavtively but doesn't work in cron.
I have also tried using the -n and -o batchmode=yes options.

When ran with cron it authenticates successfully but the command doesn't run on the server.

Any help would be great.

MS3FGX 11-22-2007 11:40 PM

Most problems with Cron scripts are caused by the fact that Cron executes the scripts in a reduced shell, not the standard login shell. So something that works from the terminal or when run in a Bash script may not work when run from Cron. To resolve this, you have to give the full path to all binaries (except for the core utilities).

So for example, you would find where the ssh binary is installed:

Code:

tj@T-Bird:~$ which ssh
/usr/bin/ssh

And then modify your script to call it directly:

Code:

#!/bin/sh

# Path to ssh binary
SSHPATH="/usr/bin/ssh"

$SSHPATH -i /home/user/.ssh/sshkey -l username server cat file.txt > /home/user/Desktop/file2.txt

Again, that is the most common problem in situations like these with Cron. There might be some SSH-specific issue that I am not thinking of here that is causing the actual problem. But this is certainly worth a shot before you take the diagnosis any farther.

acid_kewpie 11-23-2007 01:41 AM

not a networking question. moved to linux - software.

just1812 11-23-2007 06:17 PM

Thanks for your response!

I did try using the full path it never resolved my issue but I think it put me on the right path. I executed env from cron and piped it to a file. I compared the two environments and noticed that cron actually executes in /bin/sh I think this is the bourne shell. I know there is a way of setting the environment variables but I'm not sure how to off hand how to. I will look around and try to find out how to do this. Do you think this is the correct way to approach resolving this issue. I have tried many things and can't remember having such a hard time with what seems to be a very simple task.


All times are GMT -5. The time now is 10:35 PM.