I want to execute a script and have it run a command every x minutes.
Also any general advice on any resources for learning bash scripting could be really cool. I use Linux for my personal development work, so bash scripts are not totally foreign to me, I just haven't written any of my own from scratch.
Run a Linux Command Every One Minute To specify the time interval for a periodic run, use the -n option. For example, to run a Linux command or program every 1-minute use the following command. This will obviously run the 'ps -ef' command every 1 minute.
On Windows, the simplest way of running a program at startup is to place an executable file in the Startup folder. All the programs that are in this folder will be executed automatically when the computer opens. You can open this folder more easily by pressing WINDOWS KEY + R and then copying this text shell:startup .
How To Run a Command Multiple Times in Bash. Wrap your statement for i in {1..n}; do someCommand; done , where n is a positive number and someCommand is any command. To access the variable (I use i but you can name it differently), you need to wrap it like this: ${i} . Execute the statement by pressing the Enter key.
If you want to run a command periodically, there's 3 ways :
crontab
command ex. * * * * * command
(run every minutes)while true; do ./my_script.sh; sleep 60; done
(not precise)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/
In addition to @sputnick's answer, there is also watch
. From the man page:
Execute a program periodically, showing output full screen
By default this is every 2 seconds. watch
is useful for tail
ing logs, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With