LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-10-2017, 02:46 PM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Problem with watch and netstat -i command within a script


Hello --

I am writing a bash shell script that is intended to run the watch and netstat commands for ten seconds, and output the results to a text file. The operating system in question is the Red Hat Enterprise Linux 7.2 release. The script is shown below:

Code:
#!/bin/bash

# The purpose of this script is to run the netstat -i command, via the watch utility
# and output the results to a text file. This file, in turn, will be emailed to the
# system administrator.

# Change to the root account's home directory.
cd /root

# Remove the existing netstat_output text file.
rm -rf netstat_output.txt

# Run the command listed below to generate the output file.
timeout 10s watch -t -n 10 "(date '+TIME:%H:M:%S' ; ps aux | netstat -i) | tee -a /root/netstat_output.txt"
When I interactively run the command that starts with 'timeout' the output file is generated successfully. However, when the same command is run in the script, I need to kill the script, and the output file is not generated.

What do I need to change in order to get the command to work within the script?

Thanks.
 
Old 05-11-2017, 09:05 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
It seems counter-intuitive but the man page says you have to use the "--foreground" option if you're not running from command line.

So this works:
Code:
timeout --foreground 10s watch -t -n 10 "(date '+TIME:%H:M:%S' ; ps aux | netstat -i) | tee -a /root/netstat_output.txt"
I notice you have an issue in your time spec - you need a % in front of M like you have in front of H and S

Also I think you meant to put a semi-colon rather than a pipe sign (|) after ps aux.

When I tested with that it did output the ps output to the file but didn't do the netstat for some reason.

I'd suggest you only want the watch on the nestat so should separate the date and ps aux lines like so:

Code:
#!/bin/bash

# The purpose of this script is to run the netstat -i command, via the watch utility
# and output the results to a text file. This file, in turn, will be emailed to the
# system administrator.

#!/bin/bash

# The purpose of this script is to run the netstat -i command, via the watch
# utility and output the results to a text file. This file, in turn, will be
# emailed to the system administrator.

OUTPUT=/root/netstat_output.txt

# Change to the root account's home directory.
cd /root

# Remove the existing netstat_output text file.
rm -rf $OUTPUT

date +TIME:%H:M:%S >$OUTPUT
ps aux >>$OUTPUT

# Run the command listed below to generate the output file.
timeout --foreground 10s watch -t -n 10 "(netstat -i) | tee -a /root/netstat_output.txt"
Notice the single redirect (>) after date - that tells it to overwrite the contents of the file so you don't really need to do the rm -rf above that. The double redirect (>>) on the ps line tells it to append. The "-a" flag of tee also tells it to append.

Last edited by MensaWater; 05-11-2017 at 09:08 AM.
 
Old 05-11-2017, 10:01 AM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

Thank-you for your reply. I implemented the changes you suggested, and the script runs successfully. The next issue that I am encountering is when I set up
a cron job to run the script, the job fails. Surprisingly, there is no information as to why it failed.
 
Old 05-11-2017, 10:26 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
Have a look at this blog post:
http://www.linuxquestions.org/questi...-script-36931/
 
  


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
Bash script to watch a file for changes and run command reevv Programming 12 08-27-2016 04:42 AM
help with watch command with awk problem ! dr.x Linux - Server 1 11-10-2013 06:55 AM
Watch command in a shell script hawk__0 Programming 5 03-03-2010 12:42 PM
netstat command sunils1973 Linux - Networking 3 11-24-2009 07:59 AM
Netstat command Panagiotis_IOA Linux - Networking 2 03-13-2006 12:56 PM

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

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