bash script works from command line but not crontab?
The following works fine from the command line but fails when I attempt running it either as a CGI or from crontab.
My preference would be to run it as a CGI. Code:
#!/bin/bash |
Try using the full path to the executables as crontab generally runs with a limited path.
|
It is
*/5 * * * * sudo /home/pi/Develop/bash/statusAll.sh |
Inside the script, ie. /usr/bin/ssh
|
I misunderstood, sorry. However I have made the alteration you suggested and it still does not work. I get an email from root at the server where that task should be running from and the body of that email contains
Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password) but if I ssh into that server and run script from command line as user pi it works fine. Could it be that crontab entries run program under a different user? |
Try to debug your script:
Code:
#!/bin/sh |
Thanks for you suggestion and the following is what I get in the debug.log
/home/pi/Develop/bash/status.sh > /home/pi/Develop/bash/output.inc + /home/pi/Develop/bash/status.sh /usr/bin/ssh pi@192.168.0.12 /bin/sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc + /usr/bin/ssh pi@192.168.0.12 /bin/sh -s Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). /usr/bin/ssh pi@192.168.0.13 /bin/sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc + /usr/bin/ssh pi@192.168.0.13 /bin/sh -s |
Ok, so looks like you've keypair based SSH and are trying to run remote commands on other servers.
For this case you should specify the path to your key file in the ssh line. Code:
/usr/bin/ssh -i /path/to/id_rsa_or_dsa pi@192.168.0.12 [etc....] |
If I may add a question:
When you run it from crontab, is it the SAME users crontab that executes it on the command line? |
I added what I understood your suggestion to be and now I get
/home/pi/Develop/bash/status.sh > /home/pi/Develop/bash/output.inc + /home/pi/Develop/bash/status.sh /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.12 /bin/sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc + /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.12 /bin/sh -s Warning: Identity file /home/pi/.ssh/authorized_keys not accessible: No such file or directory. Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.13 /bin/sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc + /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.13 /bin/sh -s Warning: Identity file /home/pi/.ssh/authorized_keys not accessible: No such file or directory. Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.14 /bin/sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc + /usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.14 /bin/sh -s Warning: Identity file /home/pi/.ssh/authorized_keys not accessible: No such file or directory. Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). |
Quote:
If not, then what were the exact options you supplied to ssh-keygen when you created the key pair and in which directory did you run it? |
Which machine are we speaking of?
The machine on which I have the crontab running has:- ls -al .ssh/ total 20 drwx------ 2 pi pi 4096 Dec 30 15:33 . drwxr-xr-x 38 pi pi 4096 Dec 30 16:23 .. -rw------- 1 pi pi 1679 Nov 14 06:29 id_rsa -rw-r--r-- 1 pi pi 399 Nov 14 06:29 id_rsa.pub -rw-r--r-- 1 pi pi 3770 Sep 27 14:07 known_hosts and the machine where I want the script to run has:- ls -al .ssh/ total 24 drwx------ 2 pi pi 4096 Nov 14 06:35 . drwxr-xr-x 35 pi pi 12288 Dec 28 13:36 .. -rw-r--r-- 1 pi pi 399 Nov 14 06:35 authorized_keys -rw-r--r-- 1 pi pi 1554 Jul 31 07:48 known_hosts I cannot remember what the ssh-keygen command was and history does not go far enough back. Shall I redo the ssh-keygen? If so what do you recommend the ssh-keygen should be? |
As per my post, on the machine that the cron is running you specify the full path to id_rsa and the job must be running in either user root or user pi's cron.
|
Quote:
|
Ok. There is only one key pair present in ~/.ssh/ on the machine running the cron job. You can tell the SSH client to use that particular key by passing its name to ssh:
Code:
/usr/bin/ssh -i /home/pi/.ssh/id_rsa pi@192.168.0.12 ... |
All times are GMT -5. The time now is 03:44 PM. |