LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2012, 03:41 PM   #1
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Rep: Reputation: Disabled
Different output to file when running scritp from crontab


Hello,

I am trying to get the uptime from some linux servers and redirecting the information to a file. After a day of going back and forward I was able to create the scritip that would do what I wanted.

The problem I am having is that when I schedule the script to run from cron it will not give me the same output.

script:

#!/bin/bash
#variables
file="/srv/samba/share/info.txt"
#writing this server uptime to file
echo "$HOSTNAME Uptime" > $file
uptime >> $file
#connecting to other servers and getting uptime then redirecting to file
expect -c 'spawn ssh root@kcflsvlap01 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file
expect -c 'spawn ssh root@kcflsvlap02 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file
expect -c 'spawn ssh root@kcflsvldb01 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file
expect -c 'spawn ssh root@kcflsvldb02 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file
expect -c 'spawn ssh root@kcflsvltstap01 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file
expect -c 'spawn ssh root@kcflsvlnm01 uptime ; expect password ; send "passwordhere\n" ; interact' | grep -v "^root" >> $file

Results after running from terminal:

kcflsvlnm03 Uptime
16:36:03 up 31 min, 1 user, load average: 0.02, 0.03, 0.02

spawn ssh root@kcflsvlap01 uptime
16:36:03 up 15:35, 0 users, load average: 0.03, 0.04, 0.05
spawn ssh root@kcflsvlap02 uptime
16:33:05 up 106 days, 7 min, 1 user, load average: 0.10, 0.04, 0.01
spawn ssh root@kcflsvldb01 uptime
16:36:05 up 16:35, 0 users, load average: 0.18, 0.11, 0.06
spawn ssh root@kcflsvldb02 uptime
16:30:58 up 314 days, 9:07, 1 user, load average: 0.00, 0.00, 0.00
spawn ssh root@kcflsvltstap01 uptime
16:35:47 up 6 days, 16:36, 2 users, load average: 0.00, 0.03, 0.01
spawn ssh root@kcflsvlnm01 uptime
16:35:36 up 14 days, 3:48, 0 users, load average: 0.10, 0.05, 0.06

results from cron:

kcflsvlnm03 Uptime
16:38:01 up 33 min, 1 user, load average: 0.00, 0.02, 0.01
spawn ssh root@kcflsvlap01 uptime
spawn ssh root@kcflsvlap02 uptime
spawn ssh root@kcflsvldb01 uptime
spawn ssh root@kcflsvldb02 uptime
spawn ssh root@kcflsvltstap01 uptime
spawn ssh root@kcflsvlnm01 uptime

it is missing the uptime data!!!!
please any help is appreciated I dont know what to do now
 
Old 12-11-2012, 05:59 PM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Did you get E-Mailed any output from the cron job?
 
Old 12-12-2012, 08:31 AM   #3
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for the reply,

No, when I log in as root it doesnt show that I have mail.

when I redirect to a log file like this:

Code:
* * * * * /usr/local/sbin/statuscheck.sh >> /srv/samba/share/log.log 2>&1
cron doesn't log anything.

Last edited by junda; 12-12-2012 at 10:11 AM.
 
Old 12-12-2012, 10:14 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS
Posts: 1,027

Rep: Reputation: 55
Quote:
Originally Posted by junda View Post
Hello,

I am trying to get the uptime from some linux servers and redirecting the information to a file. After a day of going back and forward I was able to create the scritip that would do what I wanted.

The problem I am having is that when I schedule the script to run from cron it will not give me the same output.
Try running a script that does nothing but execute "set | sort" from both the CLI and as a cron job.

Scripts that run as cron job do necessarily have the same shell environment as you see when using the CLI; your profile (.profile, etc.) does not get sourced when you execute something under cron. I suspect the difference in output you're seeing is due to something that different in the two environments. The comparing the two outputs from this test script will allow you to see what's different and recreate whatever's missing in the script that your executing.

Hope this helps...

--
Rick
 
Old 12-12-2012, 10:41 AM   #5
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
thank you for the tip:

when I run "set | sort" from cron what I get is:

HOME='/root'
IFS='
LOGNAME='root'
OPTIND='1'
PATH='/usr/bin:/bin'
PPID='9758'
PS1='# '
PS2='> '
PS4='+ '
PWD='/root'
SHELL='/bin/sh'

when I run it from the command line one of the things I get different is SHELL=/bin/bash

Could that be the issues? what should I add to the script for cron to use /bin/bash instead of /bin/sh?
 
Old 12-12-2012, 11:43 AM   #6
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
Ok, I did the following and nothing worked:

* changed the SHELL=/bin/sh to SHELL=/bin/bash in the /etc/crontab file and restarted cron service but didnt work
* added "SHELL=/bin/bash in the first line of the crontab jobs but didnt work
* ran the cron job as follows "* * * * * /bin/bash /usr/local/sbin/statuscheck.sh" didnt work

any other sugestions????

there has to be a way to make it work....
 
Old 12-12-2012, 01:00 PM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
(1) Output file i.e. $file is already get updated with all info. in you script itself, then you need not to redirect output in same file again while declaring it in crontab, and also no need to use 2>&1.

(2) Crontab entry also doesn't look ok. All * means every minute of every hour of every week day of every month... isn't it? So make sure that at what time you want to run it.
Code:
* * * * * /usr/local/sbin/statuscheck.sh >> /srv/samba/share/log.log 2>&1
(3) Suppose if you want to run this script every 15 min, then make cron entry like:
Code:
*/15 * * * * /usr/local/sbin/statuscheck.sh
(4) Specify - Do you want to get a mail eerytime your script runs? and which distro you're using?

Last edited by shivaa; 12-12-2012 at 01:02 PM.
 
Old 12-12-2012, 01:38 PM   #8
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
* you are right about the output file i removed that from the cron job
* cron job is running at the times I want and updating the output file. I am not running it every minut of every hour etc.... that was just to ilustrate the cron job
* I do not care about getting a mail at all, all I want is for the script to run in cron the same way it runs in the command line.
I think that for some reason cron is not sending the password, since thats as far as it writes to the file:

this is what I get for each server once I remove the grep filtering from the script:

spawn ssh root@servername uptime
root@servername's password:

Last edited by junda; 12-12-2012 at 01:39 PM.
 
Old 12-12-2012, 04:54 PM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS
Posts: 1,027

Rep: Reputation: 55
Quote:
Originally Posted by junda View Post
I think that for some reason cron is not sending the password, since thats as far as it writes to the file:

this is what I get for each server once I remove the grep filtering from the script:

spawn ssh root@servername uptime
root@servername's password:
I tried a subset of your series of expect commands and ran that as a cron to grab the uptime string from a local system and got something similar to what you are seeing:
Code:
spawn ssh root@host-on-our-lan
Password: spawn uptime
I'm not sure what's causing this. A couple of things I've always watched out for in expect scripts are: a.) be sure you match the case of the string your expecting -- for example, if you're not sure whether a string is capitalized or not try looking for "assword" and b.) if the string is complicated enough you can include wildcards and use a pattern like "*assword*".

Oddly, it doesn't seem to matter if I use 'expect password', 'expect Password', or 'expect "*assword*"', I get the same output from uptime (when run interactively) even though the actual password prompt is "Password: ". The first one returned the uptime string in about 10 seconds and the second version took 0.5s. The third one matched -- again in 0.5s -- because of the wildcards. I think the 10 second delay is the default timeout value for expect. Now I'm trying to figure out what is going on with the authentication. I would have expected the first spawn to fail during the ssh connection and go ahead and issue the "uptime" command and probably return the uptime of the local host. But it didn't fail; there are entries in the remote side's /var/log/messages corresponding to the ssh connections attempted by the expect command. And the uptime numbers are from the remote host.

Are you seeing anything like that?
 
Old 12-12-2012, 05:02 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,453

Rep: Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116
Try changing the top of your script thus
Code:
#!/bin/bash
set -xv
and capture the output.
The set cmd shows exactly what the (bash) parser is doing.
Have a look to see if 'expect' has a similar debugging option.
 
Old 12-12-2012, 05:03 PM   #11
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
Yes that is exactly what I get...

I do not have any messages regarding ssh connection not even when I succesfully connected. I dont know if I am looking at the right log, I am new at linux.

But yes, it looks like the script runs and doesn't fail but the output file doesn't have the info, this only happens when runing the script from cron.
 
Old 12-12-2012, 08:49 PM   #12
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Usually, by default, output from a command run from a crontab is E-Mailed, such as with sendmail, to the "owner" of the crontab, unless that's overridden by setting the MAILTO variable.

Are you using local E-Mail facilities at all on your system, such as sendmail to transfer the E-Mail, and something like mailx to read it, with the MAIL shell variable set when you log in as root? You might want to take a look at the system logs to see if there are complaints that E-Mail can't be sent, as well as to see if there might be other related errors.

Also, some programs, which expect a "controlling terminal", may not behave well when there is no such thing available.

Output from crontabs can instead be sent to syslog with the -s option. When you appear to get no output, you might want to try the -s option on cron itself.

Often various system log files can be found in /var/log. Sometimes even if you don't know which one to examine, running a command sequence such as:

Code:
ls -lat /var/log/* | less
the moment after the cron command should have run, will show you which logs have recently been written to, as the most recently written to log files, will be near the top of the list.

Unless you are using something specific to bash, whether the Bourne shell or bash is being run, shouldn't necessarily make much difference.

Last edited by kakaka; 12-12-2012 at 08:52 PM.
 
Old 12-12-2012, 09:08 PM   #13
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
I do not care about getting a mail at all, all I want is for the script to run in cron the same way it runs in the command line.
To be honest, I am not much aware about expect cmd and it's usage. But to get a mail everytime your script runs, you can specify it in your crontab entry or in script itself, as:
If specifying in crontab:
Code:
*/15 * * * * /usr/local/sbin/statuscheck.sh | mail -s "subject" user@example.com
Or better specify in script itself (at the end):
Code:
mail -s "subject" user@example.com << $file
 
Old 12-13-2012, 03:09 PM   #14
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
Unhappy

ok,
Thank you all for all the help and time. This is what I have now:

I have reduce my script to make it easier to troubleshoot. SO I am trying it only with one server scritp looks as follows:

Code:
#!/bin/bash
file="/srv/samba/share/info.txt"
expect -c 'spawn ssh root@kcflsvltstap01 uptime ; expect password ; send "passwordhere\n" ; interact' >  $file
I added the set -xv at the begining and this is what I got:

Code:
file="/srv/samba/share/info.txt"
+ file=/srv/samba/share/info.txt
expect -c 'spawn ssh root@kcflsvltstap01 uptime ; expect password ; send "passwordhere\n" ; interact' >  $file
+ expect -c 'spawn ssh root@kcflsvltstap01 uptime ; expect password ; send "passwordhere\n" ; interact'
the set -sv got me the same results if running the script from the CML or cron....

I do not have sendmail or anything like that configured on this server so no messages....

Last edited by junda; 12-13-2012 at 03:45 PM.
 
Old 12-13-2012, 03:30 PM   #15
junda
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: Disabled
and the only thing I get from the logs is that it ran the job:

Code:
Dec 13 16:00:01 kcflsvlnm03 CRON[28559]: (root) CMD (/usr/local/sbin/statuscheck1.sh > /srv/samba/share/log.log 2>&1)
no errors, nothing else...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Crontab doesn't give the output of the file it runs in terminal pranavojha Linux - Software 3 08-24-2013 04:19 PM
send all output to file, gzip in crontab figure20012 Linux - Newbie 5 08-27-2012 04:26 AM
E-Mailing whole output of a file with crontab amario26 Linux - Newbie 1 05-03-2011 11:44 AM
Crond not reading/running /etc/crontab file [CentOS5] neocookie Linux - Software 6 09-24-2008 11:59 AM
running gpg to encrypt file from crontab aeby Linux - Security 8 05-28-2007 09:49 AM


All times are GMT -5. The time now is 03:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration