LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-13-2009, 11:02 AM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 810

Rep: Reputation: 39
Problem with output from script


Hi there --


There is a script running on several of our systems that is designed to run several commands, output the results to a file, and then e-mail the file to the system administrator. The code for the script is shown 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 -p >> dailycheck.txt
echo >> dailycheck.txt
echo >> dailycheck.txt

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-SIM3 Daily Check" admin@company.com < /root/dailycheck.txt
All the output is generated successfully with one exception: The output from the ntpq -p command does not appear in the file. The text of the file 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.

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.4G 75% /
/dev/sda1 99M 7.3M 87M 8% /boot
/dev/shm 2.0G 0 2.0G 0% /dev/shm
/dev/sda6 7.7G 563M 7.1G 8% /export/dbsroot
/dev/sda7 764M 144K 764M 1% /export/cdr
/dev/sda8 13G 1.1G 12G 9% /export/home
/dev/md0 274G 174G 100G 64% /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 13 07:00 dailycheck.txt
The output for the ntpq -p command, when run manually and either displayed on-screen or saved to file is as follows:

Quote:
remote refid st t when poll reach delay offset jitter
==============================================================================
*mcores2.mgh.har CHU_AUDIO(1) 5 u 226 1024 377 0.835 0.305 2.319
+ccores2.mgh.har CHU_AUDIO(1) 5 u 376 1024 377 0.484 -2.837 6.969
What would cause the ntpq -p output to not appear in the file that is e-mailed to the administrator, and what do I need to do in order to have it correctly appear in the file? Thanks.
 
Old 01-13-2009, 01:30 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by kaplan71 View Post
Code:
ntpq -p >> dailycheck.txt
Try
Code:
ntpq -c peers >> dailycheck.txt
If that does not work, try
Code:
ntpq -p >> dailycheck.txt 2>&1
The second version will capture any error messages.
 
Old 01-13-2009, 01:41 PM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 810

Original Poster
Rep: Reputation: 39
Hi there --

I changed the ntpq command syntax to read ntpq -c peers, and that seemed to do the trick. I read the man page concerning the -c option and peers argument.

If I understand what is being said, the -c tells ntpq the following argument is an interactive argument, while the peers argument lists the time servers in a so-called common format. Correct?
 
Old 01-13-2009, 04:14 PM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by kaplan71 View Post
If I understand what is being said, the -c tells ntpq the following argument is an interactive argument, while the peers argument lists the time servers in a so-called common format. Correct?
This is the text for the "-c" option:
Code:
-c
    The following argument is interpreted as an interactive format
command and is added to the list of commands to be executed on the
specified host(s). Multiple -c options may be given.
This is the text for the "-p" option:
Code:
-p
    Print a list of the peers known to the server as well as a summary
of their state. This is equivalent to the peers interactive command.
I created this script:
Code:
#!/bin/bash
LOG_FILE=~/lq/ntp.log
echo -n "" > $LOG_FILE
echo "#----------------------------------------" >> $LOG_FILE
echo "# Attempting \"ntpq -p\"..." >> $LOG_FILE
ntpq -p >> $LOG_FILE 2>&1
echo "# Return value is $?" >> $LOG_FILE
echo "# Done." >> $LOG_FILE
echo "#----------------------------------------" >> $LOG_FILE
echo "# Attempting \"ntpq -c peers\"..." >> $LOG_FILE
ntpq -c peers >> $LOG_FILE 2>&1
echo "# Return value is $?" >> $LOG_FILE
echo "# Done." >> $LOG_FILE
The contents of ntp.log were
Code:
#----------------------------------------
# Attempting "ntpq -p"...
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+clock.trit.net  192.12.19.20     2 u  152 1024  377   82.279    8.985   3.991
+clock3.redhat.c 66.187.233.4     2 u 1107 1024  376   77.828    8.264   0.615
-mirror          128.105.39.11    3 u  189 1024  377   50.685   -7.010   4.134
*druid.storyinme 130.207.244.240  2 u  224 1024  377   26.602   -1.512   0.946
 LOCAL(0)        73.78.73.84      5 l   49   64  377    0.000    0.000   0.001
# Return value is 0
# Done.
#----------------------------------------
# Attempting "ntpq -c peers"...
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+clock.trit.net  192.12.19.20     2 u  153 1024  377   82.279    8.985   3.991
+clock3.redhat.c 66.187.233.4     2 u 1107 1024  376   77.828    8.264   0.615
-mirror          128.105.39.11    3 u  189 1024  377   50.685   -7.010   4.134
*druid.storyinme 130.207.244.240  2 u  224 1024  377   26.602   -1.512   0.946
 LOCAL(0)        73.78.73.84      5 l   49   64  377    0.000    0.000   0.001
# Return value is 0
# Done.
Basically, the two commands are equivalent. Without looking at the source for ntpq, I cannot tell you why one works for you, but the other does not.
 
  


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
problem receiving output from Expect script slinx Linux - Software 4 04-30-2008 08:02 AM
grep and assign it's output to variables inside script itself problem xxx_anuj_xxx Programming 3 09-22-2007 11:24 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
script output spx2 Linux - Newbie 12 12-13-2005 08:44 AM
help with init script output ierickson Linux - General 0 05-07-2004 12:05 PM

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

All times are GMT -5. The time now is 06:34 AM.

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