Run a program periodically in Ubuntu

In this article, we cover how to run a program periodically in Ubuntu using watch command-line utility. There are times when we have to run a program at regular intervals. And, certainly issuing the command manually every time just isn’t a preferred option. So, there are various tools which help us automate the entire process. One such tool is – watch. This is different than scheduling tasks. When we schedule a task, it is meant to run a specific day or time.

With the help of watch command-line utility, we can run a program periodically. The output which is generated at regular intervals and is shown in full-screen mode.

Run a program periodically in Ubuntu

Use the following syntax for watch command-line utility –

watch [options] <command>

One of the options which we frequently use is -n option, it is for update intervals. The update intervals should be in seconds and no lesser than 0.1 seconds.

watch -n <seconds> <command>

For instance, if we want to run the command-line utility uptime every 5 seconds then it would be –

watch -n 5 uptime

Besides, if don’t specify time interval then it defaults to 2 seconds.

If you would like to hide time interval header then use -t option –

watch -t -n <seconds> <command>

Furthermore, we can also exit from the watch utility in case there is a change in the output. This can be achieved through -g option –

watch -g -n <seconds> <command>

Lastly, if two updates differ and we want to highlight the difference i.e. which portions of the output were updated. Then, this can be achieved through -d option –

watch -d -n <seconds> <command>

In conclusion, watch command-line utility comes handy especially if we want to want to execute a command or program at regular intervals.

Similar Posts