There are essentially 3 steps in this.
1/ Run the command in the background (&)
by running the command with an & it will run in the background. If you are running this from a terminal on the computer then that is sufficient.
If you wanted to run this from a shell that you subsequently close then you need to use nohup (information on running tasks in the background
), or if you want it to run all the time that your PC is running then you need to call it as part of the startup scripts (that topic is worthy of another thread - or google on rc.local for rpm based distros, or google init.d for any distro).
2/ Running continuously
The while loop will cause the script to keep running continuously. A "while true" is a never ending loop. See Guide to Bash Programming
(Section on loops)
3/ Pause for 2 seconds
The sleep 2 command will cause the program to pause for 2 seconds before it runs again. See man sleep
. Doing this will stop the script hogging the CPU, and meet your run every 2 seconds requirement. This time is in addition to how long it takes for your script to run, so if you script ran for 1 second, then effectively it will run every 3 seconds.