LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script works from command line but not crontab? (https://www.linuxquestions.org/questions/programming-9/bash-script-works-from-command-line-but-not-crontab-4175596437/)

stockton 12-29-2016 11:46 PM

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
/home/pi/Develop/bash/status.sh > /home/pi/Develop/bash/output.inc
ssh pi@192.168.0.12 sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc
ssh pi@192.168.0.13 sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc
ssh pi@192.168.0.14 sh -s < /home/pi/Develop/bash/status.sh >> /home/pi/Develop/bash/output.inc

The error is that connections to any other than the machine it is running on fail. In other words the SSH commands fail.

grail 12-30-2016 01:14 AM

Try using the full path to the executables as crontab generally runs with a limited path.

stockton 12-30-2016 04:37 AM

It is
*/5 * * * * sudo /home/pi/Develop/bash/statusAll.sh

grail 12-30-2016 06:05 AM

Inside the script, ie. /usr/bin/ssh

stockton 12-30-2016 06:26 AM

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?

NevemTeve 12-30-2016 06:45 AM

Try to debug your script:

Code:

#!/bin/sh

exec >/tmp/debug.log.$$ 2>&1

set -xv

... your script here ...


stockton 12-30-2016 06:54 AM

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

TenTenths 12-30-2016 07:14 AM

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....]

wpeckham 12-30-2016 07:37 AM

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?

stockton 12-30-2016 07:53 AM

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).

Turbocapitalist 12-30-2016 08:39 AM

Quote:

Originally Posted by stockton (Post 5648130)
/usr/bin/ssh -i /home/pi/.ssh/authorized_keys pi@192.168.0.12 ...

You want to point the SSH client to the actual private key instead. The file authorized_keys has a different purpose. The key pair is probably in the directory ~/.ssh/.

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?

stockton 12-30-2016 08:46 AM

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?

TenTenths 12-30-2016 08:49 AM

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.

kilgoretrout 12-30-2016 08:50 AM

Quote:

Could it be that crontab entries run program under a different user?
IIRC if you put your cron script in /etc/cron.daily, cron.monthly or cron.hourly, it runs as root and ssh is usually configured to deny root logins. That could be the reason for your Permission Denied errors.

Turbocapitalist 12-30-2016 08:52 AM

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 ...
For what it's worth, you can name the keys anything you want if it helps you remember what they are for. If you do this when creating them with ssh-keygen then it is the -f option. You can put a comment inside the public key with the -C option. Later, if you have more than one key, this helps a lot.


All times are GMT -5. The time now is 03:44 PM.