LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2016, 11:46 PM   #1
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Rep: Reputation: 2
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.
 
Old 12-30-2016, 01:14 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Try using the full path to the executables as crontab generally runs with a limited path.
 
Old 12-30-2016, 04:37 AM   #3
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
It is
*/5 * * * * sudo /home/pi/Develop/bash/statusAll.sh
 
Old 12-30-2016, 06:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Inside the script, ie. /usr/bin/ssh
 
Old 12-30-2016, 06:26 AM   #5
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
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?
 
Old 12-30-2016, 06:45 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try to debug your script:

Code:
#!/bin/sh

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

set -xv

... your script here ...
 
Old 12-30-2016, 06:54 AM   #7
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
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
 
Old 12-30-2016, 07:14 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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....]
 
Old 12-30-2016, 07:37 AM   #9
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,631

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
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?
 
Old 12-30-2016, 07:53 AM   #10
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
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).
 
Old 12-30-2016, 08:39 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by stockton View Post
/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?
 
Old 12-30-2016, 08:46 AM   #12
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
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?
 
Old 12-30-2016, 08:49 AM   #13
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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.
 
Old 12-30-2016, 08:50 AM   #14
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
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.
 
Old 12-30-2016, 08:52 AM   #15
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - command works directly in command line but not in script raoulcousins Linux - Newbie 6 08-21-2013 07:43 PM
Command works when pasted at command line but not as bash script neild Programming 7 09-23-2012 07:30 AM
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration