Linux run command every

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:

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:

Sample output:

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 command in a file. To do so, run:

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.

Sample output:

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:

Here, The -d or —differences flag will highlight the differences between successive updates.

Sample output:

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 (Eg. sk).

Читайте также:  Php with openssl linux

This can be useful in multi-user system.

To display the memory details, run:

To display the output of du command every 10 seconds, you can use:

Sample output:

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.

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 [. ]

Run A Linux Command Every X Seconds Forever With While loop

Conclusion

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.

Related read:

sk

Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.

Источник

How to Run or Repeat a Linux Command Every X Seconds Forever

A system administrator often needs to run a command repeatedly in a certain periods of time. Often such tasks can be easily completed with simple cron commands. In most of the cases this should work, but the shortest period which you can run cron command is every 1 minute. Believe it or not, in many cases this is too slow.

Run Linux Command Every Second

In this tutorial, you will learn a simple scripting techniques to monitor or keep a eye on a particular command in continuously running state similar to top command (continuously monitor the process and memory utilization) for every 3 seconds by default.

We will not stop to discuss the reasons, why you would need to run commands this often. I believe everyone has different reasons for that in their daily jobs or even at home PCs and laptops.

1. Use watch Command

Watch is a Linux command that allows you to execute a command or program periodically and also shows you output on the screen. This means that you will be able to see the program output in time. By default watch re-runs the command/program every 2 seconds. The interval can be easily changed to meet your requirements.

Monitor Memory Usage

“Watch” is extremely easy to use, to test it, you can fire up a Linux terminal right away and type the following command:

The above command will check your system free memory and update the results of the free command every two seconds.

As seen per the above output, you have a header, displaying information about (from left to right) update interval, command that is being executed and current time. If you wish to hide this header, you can use the -t option.

Читайте также:  Ms sql linux console

The next logical question is – how to change the execution interval. For that purpose, you can use the -n option, that specifies the interval with which the command will be executed. This interval is specified in seconds. So let’s say you want to run your script.sh file every 10 seconds, you can do it like this:

Note that if you run the command like shown above, you will need to cd to the directory (learn Learn 15 cd Command Examples) where the script is located or otherwise specify the full path to that script.

Other useful options of watch command are:

  1. -b – creates a beep sound if the exit of the command is non-zero.
  2. -c – Interprets ANSI color sequences.
  3. -d – highlights the changes in the command output.

Monitor Logged-In Users, Uptime and Load Average

Let’s say you want to monitor logged-in users, server uptime and load average output in continuously phase every few seconds, then use following command as shown:

To exit the command, press CTRL+C .

Here, the ‘uptime’ command will run and display the updated results every 2 seconds by default.

Monitor Progress of Copy Command

In Linux, while copying files from one location to other using cp command, the progress of data is not shown, to see the progress of data being copied, you can use the watch command along with du -s command to check the disk usage in real time.

# cp ubuntu-15.10-desktop-amd64.iso /home/tecmint/ & # watch -n 0.1 du -s /home/tecmint/ubuntu-15.10-desktop-amd64.iso

If you think that the above process is too complicated to achieve, then I suggest you to go for Advance copy command, which shows progress of data while copying.

2. Use sleep Command

Sleep is often used to debug shell scripts, but it has many other useful purposes as well. For example, when combined with for or while loops, you can get pretty awesome results.

If you are new to bash scripting, you can check our guide about bash loops here.

In case this is the first time you hear about the «sleep» command, it is used to delay something for a specified amount of time. In scripts, you can use it to tell your script to run command 1, wait for 10 seconds and then run command 2.

With the above loops, you can tell bash to run a command, sleep for N amount of seconds and then run the command again.

Below you can see examples of both loops:

for loop Example

# for i in ; do echo -n "This is a test in loop $i "; date ; sleep 5; done

The above one liner, will run the echo command and display the current date, total of 10 times, with 5 seconds sleep between executions.

This is a test in loop 1 Wed Feb 17 20:49:47 EET 2016 This is a test in loop 2 Wed Feb 17 20:49:52 EET 2016 This is a test in loop 3 Wed Feb 17 20:49:57 EET 2016 This is a test in loop 4 Wed Feb 17 20:50:02 EET 2016 This is a test in loop 5 Wed Feb 17 20:50:07 EET 2016 This is a test in loop 6 Wed Feb 17 20:50:12 EET 2016 This is a test in loop 7 Wed Feb 17 20:50:17 EET 2016 This is a test in loop 8 Wed Feb 17 20:50:22 EET 2016 This is a test in loop 9 Wed Feb 17 20:50:27 EET 2016 This is a test in loop 10 Wed Feb 17 20:50:32 EET 2016

You can change the echo and date commands with your own commands or script and change the sleep interval per your needs.

Читайте также:  Virtualbox настройка linux ubuntu

while loop Example

# while true; do echo -n "This is a test of while loop";date ; sleep 5; done
This is a test of while loopWed Feb 17 20:52:32 EET 2016 This is a test of while loopWed Feb 17 20:52:37 EET 2016 This is a test of while loopWed Feb 17 20:52:42 EET 2016 This is a test of while loopWed Feb 17 20:52:47 EET 2016 This is a test of while loopWed Feb 17 20:52:52 EET 2016 This is a test of while loopWed Feb 17 20:52:57 EET 2016

The above command will run until it is either killed or interrupted by the user. It can come in handy if you need to run a command running in the background and you don’t want to count on cron.

Important: When using the above methods, it is highly recommend that you set interval long enough to give enough time of your command to finish running, before the next execution.

Conclusion

The samples in this tutorial are useful, but are not meant to completely replace the cron utility. It is up to you to find which one works better for you, but if we have to separate the usage of both techniques, I would say this:

  1. Use cron when you need to run commands periodically even after system reboots.
  2. Use the methods explained in this tutorial for programs/scripts that are meant to run within the current user session.

As always if you have any questions or comments, do not hesitate to submit them in the comment section below.

Источник

How to repeatedly run bash script every N seconds?

I have a shell script printing some statistics like disk info, memory use and so on. But it shows information only once after the script runs and exits. Can I make this script be run repeatedly (like htop for example) or something like that? I want this info to be updated every 5-10 seconds.

If it suffices to run once a minute, you could use coron to run your script. Otherwise youl have to do some loop and actually run you script like a service. stackoverflow.com/questions/3430330/…

2 Answers 2

A slight improvement to my comment: if your script exits with true (e.g. when it ends with exit 0 ), you can run

while script; do sleep 10; done 

This is the canonical way to repeat a command as long as it doesn’t fail.

@alexkowalski Perfect! The next step would be to accept the answer you like best by clicking the check mark below the arrows.

I have a python script that gives output called test.png. is it possible to change the name for every output (after 10 seconds) like test.png, tes1.png, tes2.png .

@user1883491 Yes; but that’s an entirely diffent question. You’d have to introduce a variable such as i=0 and increment it in the loop with : $((++i)) .

This one liner is superior , while sleep 1; do echo «Hi»; done — found at unix.stackexchange.com/questions/10646/…

In linux you can use the watch program to repeat an action. Assuming that script.sh is executable:

watch -n 10 path/to/script.sh 

Would run it every 10 seconds.

To make your script executable, you can use chmod +x script.sh . Don’t forget to add the shebang

to the first line (assuming that it’s a bash script).

If you’re running the script from your current directory, you can then do:

Источник

Оцените статью
Adblock
detector