Linux – SCP not working in CRON job, only manually

cronlinuxscpssh

I have a script on a remote server (bServer) which SSH's to a central server (let's call it cServer) and kicks a script which further SSH's to 2 other servers (server13 / server14). A script is run on each of these servers which produces a file.

I have DSA SSH keys set up from cServer to server13/14. cServer will then pull the produced files from server13/14 and then bServer will pull them from cServer.

This works perfectly when run manually.

However, when I schedule the job on bServer in cron, the file from server14 is not pulled onto cServer.

Here's what's more interesting: the file from server13 doesn't transfer from cServer to bServer when the the ssh or scp command to server14 is present in the same script.

If I have them in separate scripts, server13's file gets back to bServer fine but the file for server14 is only produced, and not transferred at all back to cServer.

Both server13 and server14 are running the same build of SunOS.

I've read so many different solutions for similar problems, but haven't found any resolution.

Any ideas on how to solve this?

Best Answer

When executing a job via cron, you are not retaining your environment. If you have already logged into the remote server as the user who owns the cron script, you may wish to try, for the purpose of diagnosis, to run the same script by means of the at command, which instead does.

If you have put your commands in an executable script called my_script, the following command

 at -f my_script 0936.00

will execute the command my_script at 9 hours 36 minutes. If that works, you will know that in cron you are missing some essential component of your environment.

Related Question