Repeat Command Automatically in Linux

Repeat command automatically in Linux

Watch every 5 seconds ...

watch -n 5 ls -l

If you wish to have visual confirmation of changes, append --differences prior to the ls command.

According to the OSX man page, there's also

The --cumulative option makes highlighting "sticky", presenting a
running display of all positions that have ever changed. The -t
or --no-title option turns off the header showing the interval,
command, and current time at the top of the display, as well as the
following blank line.

Linux/Unix man page can be found here

Repeat each line multiple times by using Linux

In case perl is an option:

perl -ne '@A=split;print "$A[2]\n" x ($A[1]-$A[0])' my_file.txt

For each line, it splits the line on whitespace and @A is the array holding that result.

It then prints the third element in the array ($A[2]) + a newline, repeated (the x) the number of times you have if you take the value in column 2 minus the value in column 1.

Terminal repeat command, with variable, and new output name

The easiest way in bash would be to use a C-style for loop to iterate over the desired values of x (for the geometry) and i (to generate an output files).

for ((i=0, x=0; i<50; i++,x+=3)); do
printf -v output 'composite%03d.jpg' "$x"
composite -geometry +"$x"+20 foreground.jpg background.jpg "$output"
done

You can make it POSIX-compliant without too much effort, though at the cost of needing to run printf in a subshell.

i=0 x=0
while [ "$i" -lt 50 ]; do
output=$(printf 'composite%03d.jpg' "$x")
composite -geometry +"$x"+20 foreground.jpg background.jpg "$output"
: $((i+=1)) $((x+=3))
done

How can I make a bash command run periodically?

If you want to run a command periodically, there's 3 ways :

  • using the crontab command ex. * * * * * command (run every minutes)
  • using a loop like : while true; do ./my_script.sh; sleep 60; done (not precise)
  • using systemd timer

See cron

Some pointers for best bash scripting practices :

http://mywiki.wooledge.org/BashFAQ

Guide: http://mywiki.wooledge.org/BashGuide

ref: http://www.gnu.org/software/bash/manual/bash.html

http://wiki.bash-hackers.org/

USE MORE QUOTES!: http://www.grymoire.com/Unix/Quote.html

Scripts and more: http://www.shelldorado.com/

Linux - Do a command repeatedly for a certain length of time

Use timeout:

timeout 1800 watch -n 5 "ls" 

Repeat unterminated command every x interval of time

Use the timeout command

while true; do timeout 60 command; done

Note that if the command exits before the 60 seconds are up, it will re-execute immediately rather than waiting for the minute to be up.



Related Topics



Leave a reply



Submit