how-to: repeat OR iterate shell OR bash command delay OR interval
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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@[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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.