LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-17-2007, 02:42 PM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
Creating an application loop


Hi! Can someone point me on a way to create a command that continuously runs for every second then sleeps when told. For example
I would like to run netstat -i for every second and output the output to a file and terminate when I tell it to. I tried this with cron but would like a program that just loops until I tell it to stop

Thanks.
 
Old 01-17-2007, 02:50 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
In the case of the netstat utility it has an option to do what you want.
Code:
netstat -c
Also, if you used cron to invoke netstat every second it would probably end up taking more than one second to start and run and output. Therefore you could end up having more than one instance running which could mess up your log file or could have file locking problems. So you probably want do this:
Code:
netstat -i -c >logfile.txt
I found this information using the man page for netstat.
Code:
man netstat

Last edited by stress_junkie; 01-17-2007 at 02:52 PM.
 
Old 01-17-2007, 03:03 PM   #3
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks for the reply, I really am looking for more of an application loop than anything. I need to run it on other commands other than netstat that do not have the count option. I would rather learn how to script out a program that runs continuously until stopped.

Thanks.
 
Old 01-17-2007, 03:46 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
"watch" might do what you want, although I don't think there's an option to make it put the output in a file. You can do this by adding tee:
Code:
watch -n 5 'netstat -i |tee -a my.log'
Replace "netstat -i" with whatever command you like.
 
Old 01-17-2007, 09:12 PM   #5
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks tee might work however can you give me advice on how to append date to each of the outputs so I can tell each 2 second interval. I used the following watch -n 1 'netstat -i | tee -a | date +%r >> my.log' how do I append a date to the output of each instance of the netstat -i?
 
Old 01-17-2007, 11:02 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
This is probably what you're looking for:
Code:
watch '(date ; netstat -i; echo "") |tee -a log'
The echo "" command just adds a blank line after the netstat output to help group the date with the correct run of the program.
 
Old 01-19-2007, 10:26 AM   #7
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks A lot this works perfectly.
 
  


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
Creating my own Application Cursor pihu Linux - General 3 09-02-2005 02:53 AM
Creating a wizard like application Diederick Programming 1 07-07-2005 12:58 AM
Creating a Multithreaded Application kamransoomro84 Programming 3 06-03-2004 08:55 AM
trouble creating a link to an application evilmrhenry Linux - Software 3 02-15-2004 01:19 AM
Creating Standalone Application using Octave swifty99uk Linux - Software 0 10-28-2003 03:26 AM

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

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