LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-16-2005, 11:48 PM   #1
admarshall
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Rep: Reputation: 0
how-to: repeat OR iterate shell OR bash command delay OR interval


Is there already one bash command to do what the following script does (poorly or incompletely), ie repeat "command" indefinitely every "x" seconds:

#!/bin/sh
# usage: repeat [x] <command>
while true ; do $2 ; sleep $1 ; done


if not, and you want to use the above in a script of your own, just copy the above 3 lines and do the following (typing the lines into the console or pasting them in shell, using konsole under kde or gpm under a tty shell; hit <Ctrl-c> to stop it from looping). The two examples are optional. The stuff in angle brackets, <>, is stuff you won't see in your shell output:

am@[bin]$ cat >repeat
#!/bin/sh
# usage: repeat [x] <command>
while true ; do $2 ; sleep $1 ; done
am@[bin]$ chmod +x repeat
<examples>
am@[bin]$ ./repeat 2 "cat /proc/loadavg"
0.46 0.92 1.72 9/125 24171
0.46 0.92 1.72 2/125 24173
0.59 0.94 1.72 1/125 24181
<Ctrl-c>
am@[bin]$ cd ..
am@[~]$ repeat 2 uname
Linux
Linux
<Ctrl-c>
 
Old 07-17-2005, 12:00 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
watch -n 2 "uname -a"
 
Old 07-17-2005, 12:30 AM   #3
admarshall
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
that's great to know about. thanks, tinkster.

but i really don't want to switch to full-screen mode as "watch" does.

PS: i hit the "Thanks" button, but don't have a cc, paypal or other to plug in the min $5
 
Old 07-17-2005, 01:02 AM   #4
admarshall
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
actually, to be more specific, what i'm trying to do is something like this --
though i'm screwing up on quoting or something

am@[~]$ repeat 2 "echo $(cat /proc/loadavg ; date +%H:%m:%S)"
0.05 0.14 0.24 6/127 27711 12:07:29
0.05 0.14 0.24 6/127 27711 12:07:29
0.05 0.14 0.24 6/127 27711 12:07:29

as you can see, only one instance of load average and time are repeated.
i want a running record that can be redirected to a file
 
Old 07-17-2005, 01:14 AM   #5
admarshall
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
my apologies, if necessary. but it's lunchtime in saigon and i'm STARVING

i'll check back later

thanx again
 
Old 07-18-2005, 10:47 PM   #6
admarshall
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
I worked out a cheap hack for what i wanted. But thanks all the same...

In case you're interested:

am@[sh]$ rewatch 2 'cat /proc/loadavg'
0.51 0.36 0.65 1/121 9072 09:56:08
0.51 0.36 0.65 1/121 9083 09:56:10
0.47 0.36 0.65 1/121 9088 09:56:13

am@[sh]$ rewatch 2 'cat /proc/loadavg' 'mozilla' | tee rewatch-mozilla
0.43 0.35 0.65 1/121 9100 09:56:20
am 32106 0.0 0.3 3832 988 ? S 07:37 0:00 /bin/sh /usr/bin/mozilla
am 32112 10.0 25.7 141688 66084 ? Sl 07:37 13:54 /opt/mozilla/lib/mozilla-bin
0.40 0.34 0.64 3/121 9109 09:56:22
am 32106 0.0 0.3 3832 988 ? S 07:37 0:00 /bin/sh /usr/bin/mozilla
am 32112 10.0 25.7 141688 66092 ? Sl 07:37 13:54 /opt/mozilla/lib/mozilla-bin

am@[sh]$ cat ~/bin/rewatch
#!/bin/sh
# rewatch, v. 0.01
#
# Usage: rewatch <n> <cmdline> [process-to-watch]
# n: seconds between repetitions
# cmdline: command-line to repeat
# process-to-watch: process for which "ps aux" info is to be displayed
#
# Purpose: repeat and, optionally, watch and record the "ps aux" info of
# that or any other process
#
# Bkgd: original purpose was to be able to input a command-line (in single
# quotes) along with an interval in seconds for its repetion and print a
# list of its stdouts labelled by the times they were printed until <Ctrl-c>
# is hit, and be able to capture all the output to stdout or a file.
#
# initial use was for watching and recording load averages as programs were
# loaded, eg, "re_watch 2 'cat /proc/loadavg' konqueror"
#
# Future: unknown
#
# Known Bugs: no input validation or user feedback for input errors
#
# Of course, bash operators will have to be properly quoted or escaped or
# not... -- which i still don't clearly understand

n=$1
cmdline=$2
proc2watch=$3

if test -z "$3"

then
while true ; do echo $( $cmdline && echo "$( date +%H:%M:%S )" ) ; sleep $n ; done

else

while true ; do echo $( $cmdline && echo "$( date +%H:%M:%S )" ) ; echo "$( ps aux | grep -vE "(rewatch |grep |tee )" | grep $proc2watch )" ; sleep $n ; done

fi
am@[sh]$
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about the bash shell command naihe2010 Programming 7 10-27-2005 12:40 AM
mouse scrool and keyboard delay/repeat monohouse Linux - Software 3 11-02-2004 07:34 AM
Bash shell command on windows98 emailssent General 3 10-16-2004 06:25 AM
How do you set the keyboard wait-until-repeat delay? deanbrown3d Linux - Newbie 2 06-18-2004 09:35 AM
Bash: Iterate through $@ (easy++) beatnik007 Programming 2 04-03-2004 01:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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