• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to run Linux commands every X seconds

82

How to run Linux commands every X seconds

In this article we will explain how to run Linux Linux commands every X seconds and how System administrators runs commands for certain period of time.

Options of watch command

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

Using Watch Command

It helps to execute the commands every 2 seconds.

To Monitor Memory usage

watch free -m

Output

Every 2.0s: free -m                                     Sat Mar  5 00:01:59 2016

                     total    used       free     shared     buffers     cached
Mem:                 988        533        455      3          32          201
-/+ buffers/cache:   299        688
Swap:                1999       0          1999

-n option is used to specify the interval when the command will be executed. -d option is used to highlight the changes in the output.

Every 5.0s: sh script.sh                                Sat Mar  5 00:13:55 2016

total 596
-rw-rw-r-- 1 user1 user1     14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1  297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1  304451 Mar  5 00:13 watch 2.png
                        total         used        free     shared    buffers     cached
Mem:                    988          545         443      4         33          208
-/+ buffers/cache:      303          684
Swap:                   1999         0           1999

To monitor logged-in users

Example

[user1@linuxhelp ~]$ watch uptime

Every 2.0s: uptime                                      Sat Mar  5 00:17:32 2016

00:17:32 up  1:18,  2 users,  load average: 0.00, 0.00, 0.00

To exit the command

Press CTRL+C

The up time command will run and display the updated commands results every 2 seconds.

To monitor progress copy command

Example

[user1@linuxhelp Desktop]$ cp ubuntu-10.04.1-server-amd64.iso /home/user1/new/ 
[user1@linuxhelp Desktop]$ watch -n 2 du -s /home/user1/new/
Every 2.0s: du -s /home/user1/new/                  Sat Mar  5 00:31:10 2016

698880  /home/user1/new/

Using sleep Command for loop

Example

[user1@linuxhelp ~]$ for i in {1..3}  do ll   date   sleep 3   done
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:35 IST 2016
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:39 IST 2016
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:42 IST 2016

Using while loop

Example

[user1@linuxhelp ~]$ while true  do ll   date   sleep 3  done 
total 700444
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
-rw-rw-r-- 1 user1 user1    374098 Mar  5 00:51 watch 5.png
Sat Mar  5 00:57:23 IST 2016
total 700444
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
-rw-rw-r-- 1 user1 user1    374098 Mar  5 00:51 watch 5.png
Sat Mar  5 00:57:26 IST 2016
total 70044

Tags:
jackson
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

I want this command
# cat /proc/mdstat
to run every 2 sec automatically and repetitively how can it be done?

A

You can make use of the following command
watch -n 2 cat /proc/mdstat

Q

How to Run a command after a delay of n mins?

A

use the following command
'sleep n && command' or 'at'

Q

How to Repeat a Unix command every x seconds forever?

A

watch is the best command to Repeat a Unix command every x seconds forever

Q

Can you tell me "Is there any command in solaris that gives the output repeatedly for every x seconds" when used with other commands like ls,du,df,etc..Like prstat updates its output for ever

A

Use the following command
yes "; sleep 5" | sh

Q

How to Monitor Memory usage from command line?

A

To Monitor Memory usage from command line use the following command

#free -m

Related Tutorials in How to run Linux commands every X seconds

Related Tutorials in How to run Linux commands every X seconds

FFmpeg Commands in Linux with Examples
FFmpeg Commands in Linux with Examples
Apr 4, 2016
Basic Linux Command Line Tricks
Basic Linux Command Line Tricks
Apr 20, 2016
Useful commands to manage Desktop and Server in Linux - Part 1
Useful commands to manage Desktop and Server in Linux - Part 1
Apr 27, 2016
Useful commands to manage Desktop and Server in Linux - Part 2
Useful commands to manage Desktop and Server in Linux - Part 2
Apr 27, 2016
How to use mkdir, tar and kill commands efficiently in Linux
How to use mkdir, tar and kill commands efficiently in Linux
Apr 19, 2016
Interesting Command Line Tips and Tricks in Linux
Interesting Command Line Tips and Tricks in Linux
Apr 5, 2016
How to run Linux commands every X seconds
How to run Linux commands every X seconds
Mar 25, 2016

Related Forums in How to run Linux commands every X seconds

Related Forums in How to run Linux commands every X seconds

CentOS
isaiah class=
pidstat no found
May 28, 2018
Basic Commands
FerTah class=
Command sort or uniq
Apr 10, 2019
Windows
Aditya class=
Net view usage in Windows 10
Jul 31, 2019
Basic Commands
gibbson class=
How to install locate command
Aug 25, 2018
Basic Commands
yousuf class=
How to check history command with time
Aug 25, 2018
Basic Commands
raven class=
Basic Commans are not working
Dec 29, 2017
CentOS
muhammad class=
list files on created time
Jun 12, 2018
Basic Commands
elijah class=
How to List only Directories from the Current Location in Linux
Dec 18, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.