Bash script that executes php file every 5 seconds

后端 未结 6 1961
孤街浪徒
孤街浪徒 2020-12-22 06:09

Bash script:

$ cat test.sh
#!/bin/bash
while true
do
 /home/user/public_html/website.com/test.php
 sleep 5
done

Im trying to call it with c

6条回答
  •  不知归路
    2020-12-22 06:58

    An alternative answer is to run you scripts for 12 times only, and put it in crontab

    #!/bin/bash
    for loop in 1 2 3 4 5 6 7 8 9 0 1 2; do
      /home/user/public_html/website.com/test.php &
      sleep 5
    done
    

    -- OR --

    #!/bin/bash
    loop=0
    while [ $loop -lt 12 ]; do
      /home/user/public_html/website.com/test.php &
      sleep 5
      loop=$(($loop+1))
    done
    

    --update--

    If your goal is not to have more than one test.php, use this script: (Run it once only, do not putting it in the crontab):

    #!/bin/bash
    
    while true; do
        begin=`date +%s`
        /home/user/public_html/website.com/test.php
        end=`date +%s`
        if [ $(($end - $begin)) -lt 5 ]; then
            sleep $(($begin + 5 - $end))
        fi
    done
    

    Explanation: This script calls test.php, and wait for it to terminate (since it dose not have & in the end). Then it measure the time: if it's already passed 5 seconds, it call test.php right away. otherwise, it sleeps for the remaining time so that next test.php will be called at the next 5 seconds starting from the beginning of the previous test.php

提交回复
热议问题