![]() ![]() This while loop stores the current Unix time as currentTime, then it subtracts the last time ( lastTime) from the current time, storing it as elapsedTime. Replace X with the time interval between runs (in seconds), and command with the command you want to repeat every X seconds.Ī bit of an explanation is in order here. While true do currentTime=$(date +%s) elapsedTime=$((currentTime - lastTime)) if then lastTime=$currentTime i=0 fi done ] A solution (via stackexchange) which avoids time drifting, and doesn't have the issue I mentioned above (so no different time interval between the first and second run), but which doesn't make use of the sleep command, is the following one-liner: If you know a better way to do this, please leave a comment below. Subsequent runs use the correct time interval though. Between the first and second run, the time interval may be shorter than the one you've specified. You'll notice that the date printed in the terminal is in increments of 5 seconds, like this: While true do echo $(date) sleep 3 sleep $((5 - $(date +%s) % 5)) done ![]() Using this, even though the command ( sleep 3 in this case, it can be any command you want) takes 3 seconds to complete, the interval between runs is 5 seconds.įor testing this you can add an echo $(date) at the beginning, like this: While true do sleep 3 sleep $((5 - $(date +%s) % 5)) done The following one-liner repeats the sleep 3 command (I couldn't think of a better example right now of a command that takes a few seconds to complete and isn't destructive in any way in case the user doesn't stop it - I'm typing this at 5 AM) every 5 seconds: Read the explanation and alternative to this in the marked edits further down this article.Įxample. Here, replace command with the command you want to run, and both instances of X with the number of seconds after which the command should be repeated. ![]() While true do sleep $(( - $(date +%s) % )) done For example, if you want to run a command every 5 seconds, but that command takes 3 seconds to complete, this will cause the command to run every 8 seconds, instead of every 5 seconds.Ī solution for avoiding this time drift, and running the command every X seconds regardless of how long it takes to complete (as long as the command doesn't take more to complete than the sleep time), is to use the following: It's also important to note that using this, time will drift, depending on how long the command you're running takes to complete. If you're doing something more complicated, make sure that the interval is shorter than the time required for the command to finish, or else this might interfere with something in your script. The following one-liner runs the echo $(date) command every 5 seconds: Here, you need to replace command with the command to run, and X with the time interval in seconds (the number doesn't have to be an integer, but it can't be negative you can also specify minutes - 1m for example, hours - 1h for example, etc.).Įxample. Using sleep in a while loop to repeat a command every X secondsĪnother way of repeating a command every X seconds on Linux is to use a while loop with the sleep command: #Mac sleep command line how to#You might also like: How To Find Files Modified In The Last N Days Or Minutes Using find Check out its man page for more information. The watch command has a few options, like highlighting the differences between successive updates ( -differences / -d), beep if a command has a non-zero exit ( -beep / -b), and more. ![]() The following command runs ps aux | grep firefox every 5 seconds: If the command contains pipes, quotes or other special characters, use single quotes, e.g.: 'command'.Įxample. X is the interval at which to run the command, in seconds (with 0.1 seconds being the lowest possible value).Using watch to run a command every X seconds: This allows you to watch the program output change over time. Watch can run a command repeatedly, displaying its output and errors (the first screenful). Using watch to run a command every X seconds This article explains how to repeat a command every X seconds on Linux, in 2 ways: using watch, and using sleep in a while loop (with a way to avoid time drift when using long-running commands). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |