Repeat command automatically in Linux

前端 未结 13 1685
滥情空心
滥情空心 2020-12-04 04:54

Is it possible in Linux command line to have a command repeat every n seconds?

Say, I have an import running, and I am doing

ls -l
         


        
13条回答
  •  隐瞒了意图╮
    2020-12-04 05:04

    Running commands periodically without cron is possible when we go with while.

    As a command:

    while true ; do command ; sleep 100 ; done &
    [ ex: # while true;  do echo `date` ; sleep 2 ; done & ]
    

    Example:

    while true
    do echo "Hello World"
    sleep 100
    done &
    

    Do not forget the last & as it will put your loop in the background. But you need to find the process id with command "ps -ef | grep your_script" then you need to kill it. So kindly add the '&' when you running the script.

    # ./while_check.sh &
    

    Here is the same loop as a script. Create file "while_check.sh" and put this in it:

    #!/bin/bash
    while true; do 
        echo "Hello World" # Substitute this line for whatever command you want.
        sleep 100
    done
    

    Then run it by typing bash ./while_check.sh &

提交回复
热议问题