This question already has an answer here:

I'm having an .sh which has this particular script in it:

scp -P 22 /somepath/testfrom/* user@server:~/somepath/testo

This basically copies all the files (but then I'm having only one file for testing purposes) from one server to another. I pretty much followed this tutorial and it absolutely worked when I ran the .sh file from the command line (ie ./script.sh).

So I tried to execute this as a cron job where I appended a line in crontab -e as:

* * * * * /home/chamith/work/ELK/scripts/script.sh

I've been cracking my head on this, it didn't work at all! Where as running the script as a file from the command line worked perfectly.

Where am I going wrong? Any helpd could be appreciated.

marked as duplicate by muru bash Jan 6 '17 at 8:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • (Side note: -P 22 is redundant. 22 is assumed if no port is specified) Is your cron running it as root? – DTSCode Jan 7 '17 at 20:22
  • Usually because your scripts are not using fully-qualified filenames. Remember, all cron jobs are run as root so you cannot make any assumptions about which directory it begins with. The first command in my cron jobs is always a cd /home/pi or something similar. – SDsolar Nov 4 '17 at 7:38