LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Creating an application loop (https://www.linuxquestions.org/questions/linux-software-2/creating-an-application-loop-520250/)

keysorsoze 01-17-2007 02:42 PM

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.

stress_junkie 01-17-2007 02:50 PM

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

keysorsoze 01-17-2007 03:03 PM

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.

matthewg42 01-17-2007 03:46 PM

"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.

keysorsoze 01-17-2007 09:12 PM

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?

matthewg42 01-17-2007 11:02 PM

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.

keysorsoze 01-19-2007 10:26 AM

Thanks A lot this works perfectly.


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