0

Trying to scp a file in a bash script that's scheduled by cron. When cron triggers it, the scp fails with a permission denied error message. When I run the script manually using a command like:

/etc/scripts>  bash script3

the scp command works just fine.

From a previous question about how to troubleshoot cron jobs in general, I learned that the environment variables might be different between when i'm running via command line manually and when cron runs. So in following one of the suggested troubleshooting tip, I scheduled the following job via cron:

lab-1:/etc/scripts# cat cronenvtest         
OFILE=/tmp/crons.environment                                      
(/usr/bin/whoami                                                  
/usr/bin/env ) > $OFILE 2>&1    

The output looks like this:

lab-1:/var/www/# tail -f /tmp/crons.environment
HOME=/root
RC_SVCNAME=crond
LOGNAME=root
RC_SERVICE=/etc/init.d/crond
TERM=screen
SVCNAME=crond
PATH=/bin:/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
LANG=en.utf8
SHELL=/bin/sh
PWD=/root

When I run "env" on the command line, it looks like this:

lab-1:/tmp# env                                            
SSH_CLIENT=10.1.1.1 57728 22                                                
USER=root                                                                        
MAIL=/var/mail/root                                                              
SHLVL=1                                                                          
OLDPWD=/etc/scripts                                                              
HOME=/root                                                                       
SSH_TTY=/dev/pts/4                                                               
PAGER=less                                                                       
PS1=\h:\w\$                                                                      
LOGNAME=root                                                                     
TERM=screen                                                                      
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin                
LANG=en.utf8                                                                     
SSH_AUTH_SOCK=/tmp/ssh-eGvX0isT3V/agent.22951                                    
SHELL=/bin/ash                                                                   
PWD=/tmp                                                                         
CHARSET=UTF-8                                                                    
SSH_CONNECTION=10.1.1.1 57728 10.2.2.2 22           

Here's the line in the script that tries to do the scp:

 scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null @$server:/tmp/"$server.db" "$dir/"

What I've tried so far

I tried to change the scp command to include the root user, like so:

scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$server:/tmp/$"server.db" "$dir/"

Tried to change the destination from $dir variable to a hardcoded path like "/tmp"

Tried to add the following line to the bash script, right before the scp:

PWD=/etc/scripts

So far, none of this has worked. Any tips?

  • 2
    You have an active SSH authentication agent, which is something the cron script won't have. So run unset SSH_AUTH_SOCK and then try the scp command again. If it starts failing, it's because your SSH agent is supplying the key for the authentication. Without the agent, the scp command may need to prompt for a passphrase, or the private key might not be in place. – telcoM Mar 15 '18 at 15:23
  • I think that's what the problem is. Thanks – dot Mar 15 '18 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.