Linux: Execute a Program Periodically

Objective: Execute a program on the shell every x seconds, display the program output and highlight the changes to the output.

To execute a program at regular intervals, you can schedule it using cron or by using a simple loop.

The above loop will run a process called program every 60 seconds.

But there is another easier way of achieving this – by using the watch Linux command. The watch command is much more flexible and it can be used to highlight differences in output, send a beep if the executed command has a non-zero output, or exit when the output changes.

To check the memory usage every second, use the following syntax.

To run a program every 5 seconds and to exit when the output changes, use the following syntax. This can be used to monitor changes to the command output.

To run a program every 5 seconds and to highlight the differences between program output, use the following syntax.

To exit the watch command, use Ctrl-C.

If you are copying a very large file, you can also use the watch command to monitor the copy progress by checking the file size of the file being copied.

Once you run the above command, you will get a screen something like the one below. The file size will be updated every 0.1 second.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }