SCP not working in CRON job, only manually

7,892

Solution 1

None of the suggested solutions worked.

To fix this, I have had to have 2 seperate cron job. One which pulls the file from server14 to server13 then back to bServer. A second cron job (won't work if ran as second script in same job) then pulls the file from server13.

Very strange. Shame I couldn't find a more elegant solution though.

Solution 2

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.

Solution 3

Probably the problem is in environment variables which are used by cron. Run "env > out.txt" from cron and from shell and compare results.

Take a look at this solution https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use

You can try to add source /etc/profile at the start of your script. Also you can try to run script with #!/bin/bash -l

Share:
7,892

Related videos on Youtube

cardycakes
Author by

cardycakes

Updated on September 18, 2022

Comments

  • cardycakes
    cardycakes over 1 year

    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?

  • cardycakes
    cardycakes over 10 years
    You're right, my env_cron.out was only 4 lines in comparison the 20+ of my env.out. I copied env.out (stuck 'export' at beginning of each var) and renamed the file to set_env.sh with the obligatory #!/bin/bash. I ran this FROM my script which is kicked off via cron, and also within cron before the script... Neither worked.
  • cardycakes
    cardycakes over 10 years
    One of answers in your suggested solution talks about kicking off your .bash_profile file... But I do not have one of these.
  • cardycakes
    cardycakes over 10 years
    PS. I cannot include variables in crontab.