LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-15-2009, 10:53 AM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Correct output from script appears only when script is run interactively


Hi there --

I have a script on several of our systems that runs a series of checks, and e-mails the results to the system administrator. The workstations in question are running a derivative of Fedora Core. The code for the script is listed below.

Code:
#!/bin/bash

# This script will run the df and uptime commands,
# and output them to a file. The file, in turn, will
# be e-mailed to the system administrator.

# Change to the home directory of the root user.
cd /root

# Run the df and uptime commands, and save the output
# to a text file.
echo "The following line will be the readout for the amount" > dailycheck.txt
echo "of time the system has been up since the last reboot." >> dailycheck.txt
echo >> dailycheck.txt
uptime >> dailycheck.txt
echo >> dailycheck.txt
echo >> dailycheck.txt

echo "The information  shown below is the readout of the current" >> dailycheck.txt
echo "disk space usage on the ct-sim workstation. The most important" >> dailycheck.txt
echo "is the /export/home1 line. This filesystem is where the DICOM" >> dailycheck.txt
echo "patient data is located on the system." >> dailycheck.txt
echo >> dailycheck.txt
df -h >> dailycheck.txt
echo >> dailycheck.txt
echo >> dailycheck.txt

echo "The output shown below gives the date and time that is currently" >> dailycheck.txt
echo "on the system. The workstation should be synchronized with the network" >> dailycheck.txt
echo "time servers. The '+' and '*' symbols that precede the time servers" >> dailycheck.txt
echo "indicate the workstation is in sync with both of them." >> dailycheck.txt
echo >> dailycheck.txt
ntpq -c peers >> dailycheck.txt
echo >> dailycheck.txt
echo >> dailycheck.txt

# NOTE: Be sure to use the '-c peers' option and argument after the ntpq command. They are
# necessary for the output of the ntpq command to be saved to the dailycheck.txt file. 

echo "The file listed below should have today's date as its timestamp." >> dailycheck.txt
echo "If it does not, the administrator should investigate the cause." >> dailycheck.txt
# Run the ls -l command to list in detail dailycheck.txt, 
# and save the output to a text file
echo >> dailycheck.txt
ls -l dailycheck.txt >> dailycheck.txt

# E-mail the text file to the system administrator.
mail -s "CT-SIM1 Daily Check" admin@company.com < /root/dailycheck.txt
When the script is run interactively, the output is the following:

Quote:
The following line will be the readout for the amount
of time the system has been up since the last reboot.

11:43:32 up 10:42, 3 users, load average: 0.00, 0.00, 0.00


The information shown below is the readout of the current
disk space usage on the ct-sim workstation. The most important
is the /export/home1 line. This filesystem is where the DICOM
patient data is located on the system.

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 5.1G 3.8G 1.3G 75% /
/dev/sda1 99M 7.3M 87M 8% /boot
/dev/shm 2.0G 0 2.0G 0% /dev/shm
/dev/sda6 7.7G 390M 7.3G 5% /export/dbsroot
/dev/sda7 764M 144K 764M 1% /export/cdr
/dev/sda8 13G 501M 13G 4% /export/home
/dev/md0 274G 143G 131G 53% /export/home1


The output shown below gives the date and time that is currently
on the system. The workstation should be synchronized with the network
time servers. The '+' and '*' symbols that precede the time servers
indicate the workstation is in sync with both of them.

remote refid st t when poll reach delay offset jitter
==============================================================================
*ccores2.mgh.har CHU_AUDIO(1) 5 u 885 1024 377 4.982 -0.932 2.859
+mcores2.mgh.har CHU_AUDIO(1) 5 u 1012 1024 377 5.387 0.771 0.667


The file listed below should have today's date as its timestamp.
If it does not, the administrator should investigate the cause.

-rw-r--r-- 1 root bin 1542 Jan 15 11:43 dailycheck.txt
The script is meant to be run as a cron job and provide the same output. However, when it is run in that state, the output, as shown below, is missing the ntpq -p data.

Quote:
The following line will be the readout for the amount
of time the system has been up since the last reboot.

07:00:01 up 5:58, 0 users, load average: 0.00, 0.00, 0.00


The information shown below is the readout of the current
disk space usage on the ct-sim workstation. The most important
is the /export/home1 line. This filesystem is where the DICOM
patient data is located on the system.

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 5.1G 3.8G 1.3G 75% /
/dev/sda1 99M 7.3M 87M 8% /boot
/dev/shm 2.0G 0 2.0G 0% /dev/shm
/dev/sda6 7.7G 390M 7.3G 5% /export/dbsroot
/dev/sda7 764M 144K 764M 1% /export/cdr
/dev/sda8 13G 501M 13G 4% /export/home
/dev/md0 274G 143G 131G 53% /export/home1


The output shown below gives the date and time that is currently
on the system. The workstation should be synchronized with the network
time servers. The '+' and '*' symbols that precede the time servers
indicate the workstation is in sync with both of them.



The file listed below should have today's date as its timestamp.
If it does not, the administrator should investigate the cause.

-rw-r--r-- 1 root bin 1226 Jan 15 07:00 dailycheck.txt
I wrote another script where only the ntpq -p command was executed prior to the output being e-mailed to the administrator. As with the aforementioned script, when run interactively, the output is correct. When run as a cron job, the output is missing the ntpq data.

I tried using the ntpq -p as well as ntpq peers command syntax in the scripts, and both resulted in the issue that has been described here.

Does anyone have an idea as to why this is occurring? Thanks.
 
Old 01-15-2009, 11:05 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Try to put the full path of the ntpq command, since crontab has a very limited environment, where PATH is usually /bin:/usr/bin. To me your cron job resulted in a "command not found" error which has been sent to the crontab's owner mail, since you have not redirected the standard error of the ntpq command to a file.

The full path of ntpq is usually /usr/sbin/ntpq.
 
Old 01-15-2009, 11:47 AM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hi there --

Thanks for your reply. I made the change that you suggested to the script. I took the liberty of checking the owner's mail, and indeed there was an error message indicating that ntpq could not be found.

I'll let you know what the results after the next scheduled run.
 
  


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
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 03:29 AM
awk run script on field before output Geneset Programming 3 08-26-2008 04:59 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:41 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