How To Run A Linux Command Every X Seconds Forever
Have you ever been in a situation where you had to run a specific Linux command every few seconds repeatedly? Well, if you don’t know already, this tutorial will teach you how. Of course you can do this using a shell script or cron jobs. You can also repeat a Linux command at a particular interval without having to manually run it every time. Here is where Watch command comes in handy. This command can be used to execute the given command repeatedly, and monitor the output. To put this in simple words, we can use Watch command to run a Linux command every X seconds forever and it will keep displaying the output in the console until we stop it manually by pressing CTRL+C or kill the process or forcibly reboot the system. By default, the given command will run every 2 seconds, or you can define the time interval of your choice.
Run A Linux Command Every X Seconds Forever With Watch Command
The syntax of watch command is:
$ watch [options] command
Below I have given a few examples to teach you where you can use watch command to run a specific Linux command repeatedly.
Let us say you want to run the ‘uptime’ command every 2 seconds to monitor the uptime of your system. To do so, simply run:
$ watch uptime
Every 2.0s: uptime sk Wed Feb 9 20:14:46 2018 20:15:46 up 2:38, 1 users, load average: 0.41, 0.35, 0.46
- Every 2.0s: uptime – The ‘uptime’ command will run every 2 seconds and display the result.
- sk – The currently logged in user
- Wed Feb 9 20:14:46 2018 – The current date and time when we executed the command.
This will keep running until you manually end it. To exit the command, press CTRL+C.
You can also save the output of the uptime in a file. To do so, run:
$ watch `uptime` > uptime.txt
$ watch `uptime` > uptime.doc
This can be useful when you wanted to send the uptime of your system to a technical support for getting help.
As I mentioned before, watch command executes a program every 2 seconds by default. We can change it to a particular interval, for example 5 seconds, using ‘-n’ parameter.
Let us display the file system disk space usage for every 5 seconds.
$ watch -n 5 df -h
Every 5.0s: df -h sk: Wed May 9 20:19:09 2018 Filesystem Size Used Avail Use% Mounted on dev 3.9G 0 3.9G 0% /dev run 3.9G 1.1M 3.9G 1% /run /dev/sda2 457G 357G 77G 83% / tmpfs 3.9G 32M 3.9G 1% /dev/shm tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 3.9G 36K 3.9G 1% /tmp /dev/loop0 83M 83M 0 100% /var/lib/snapd/snap/core/4327 /dev/sda1 93M 55M 32M 64% /boot tmpfs 789M 28K 789M 1% /run/user/1000
To check whether this command really works, create or delete any file/folder. You will notice that the free space has changed in the output after creating or deleting the files/folders.
To watch contents of a directory change, run:
$ watch -d ls -l
Here, The -d or –differences flag will highlight the differences between successive updates.
total 3857440 -rw------- 1 sk users 1921843200 Apr 25 22:47 bionic-desktop-amd64.iso -rw------- 1 sk users 1921843200 Apr 25 03:02 bionic-desktop-amd64.iso.zs-old drwxr-xr-x 2 sk users 12288 May 8 18:45 Desktop drwxr-xr-x 2 sk users 4096 Apr 20 16:54 Documents drwxr-xr-x 11 sk users 4096 May 9 19:56 Downloads drwxr-xr-x 2 sk users 4096 Jan 7 2017 Music drwxr-xr-x 5 sk users 12288 Mar 23 17:34 Pictures drwxr-xr-x 2 sk users 4096 May 11 2016 Public
Also, you can display the contents of a directory change which is owned by a particular user (Ex.sk).
$ watch -d 'ls -l | fgrep sk'
This can be useful in multi-user system.
To display the memory details, run:
$ watch -d free -m
To display the output of du command every 10 seconds, you can use:
$ watch -n 10 du -h
Every 10.0s: du -h sk: Wed May 9 20:26:43 2018 17M ./.disruptive innovations sarl/bluegriffon/q87d9o6v.default/extensions 16K ./.dooble/Dooble 4.0K ./.dooble/Histories 176K ./.dooble 4.0K ./.PlayOnLinux/wine/mono 13M ./.PlayOnLinux/wine/gecko 4.0K ./.PlayOnLinux/wine/linux-amd64 652K ./.PlayOnLinux/wine/linux-x86/1.3.19/share/wine/fonts 872K ./.PlayOnLinux/wine/linux-x86/1.3.19/share/wine
This monitor the disk usage every 10 seconds until you exit it manually.
For more details, I recommend you to refer man pages.
$ man watch
Run A Linux Command Every X Seconds Forever With While loop
You can also do this with the help of While loop.
To repeat a command every 2 seconds forever, run:
$ while sleep 2 ; do uptime ; done
21:10:21 up 10:12, 1 user, load average: 0.83, 0.71, 0.59 21:10:23 up 10:12, 1 user, load average: 0.76, 0.70, 0.59 21:10:25 up 10:12, 1 user, load average: 0.76, 0.70, 0.59 21:10:27 up 10:12, 1 user, load average: 0.70, 0.68, 0.58 [...]
You know now how to run a command every X seconds using Watch command. Generally, Watch command is used for monitoring disk usage and memory usage. Please do not confuse this command with other monitoring tools. This command is intend to execute a command every particular second forever, until you manually stop it.