Bash script that executes php file every 5 seconds

后端 未结 6 1920
孤街浪徒
孤街浪徒 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:36

    You missed the & character to put the process in the background :

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

    Ensure your script is eXecutable :

    chmod +x /home/user/public_html/website.com/test.php
    

    but to combine cron and this script, there's one way :

    @reboot /path/to/script.sh
    

    because you need only one instance of your script.

    If you cron doesn't support @reboot, another possibility is to add a line in /etc/rc.local

    0 讨论(0)
  • 2020-12-22 06:44

    That cron rule will execute your script every minute. And each script will keep running an infinite loop. So you will end up with many processes..

    Unfortunately cron does not allow finer granularity than minutes, so you could leave it the way you have it, but add some locking. Locking that checks if the script isn't already running before firing up a new one.

    You could use something like 'flock', if you have it installed,

    $ flock --version
    flock (util-linux 2.20.1)
    

    And make your cron so:

    # m h  dom mon dow   command
    * * * * * flock -n /tmp/test.lock /home/user/public_html/website.com/test.sh
    
    0 讨论(0)
  • 2020-12-22 06:46

    The problem is that cron doesn't usually have the same PATH variable as your user do.

    You should use the full path to the sleep binary '/bin/sleep'. But, as other users noted, your script is going to spawn a copy of the script each minute, which may not be what you're expecting.

    0 讨论(0)
  • 2020-12-22 06:57

    Below code I've used linux & cent os servers its working for me

    Step 1 => Within this path /usr/bin create autocron.sh file -> vim autocron.sh

    Step 2 => After creating autocron.sh file -> Put this code within autocron.sh file while true; do begin=date +%s; php executablefile.php; end=date +%s; if [ $(($end - $begin)) -lt 5 ]; then sleep $(($begin + 5 - $end)); fi; done -> Example excutable PHP file name & .sh file shouble be the same .

    Step 3 => After that save that autocron.sh file

    Step 4 => Give the permissions for autocron.sh file -> chmod +x autocron.sh

    Step 5 => And also give the permissions for PHP file -> chmod +x PHP file location (executablefile.php)

    Step 5 => This command will runs the .sh file in background process even terminal was closed -> nohup sh autocron.sh&

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-22 07:02

    to make the cron job work (executable) do a

    $ chmod +x /home/user/public_html/website.com/test.sh
    

    to make the bash script call the php

    $ chmod +x /home/user/public_html/website.com/test.php
    

    finally your php file should have a shebang copy paste this code to top of php file

    #!/usr/bin/php
    

    or without shebang you need to change you shell script call

    php /home/user/public_html/website.com/test.php
    
    0 讨论(0)
提交回复
热议问题