[SOLVED] bash script works from command line but not crontab?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105
Rep:
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.
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105
Original Poster
Rep:
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?
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?
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105
Original Poster
Rep:
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.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.