user2979496
user2979496

Reputation: 7

Bash script that executes php file every 5 seconds

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 cron:

* * * * * /home/user/public_html/website.com/test.sh

Where is my mistake? Does it matter where the .sh is located?

EDIT: I fixed all the problems from above. I also created this cron job to prevent multiple proccesses:

* * * * * flock -n /tmp/test.lock /home/user/public_html/website.com/test.sh

The problem now is that the bash executes the php every 5 seconds and this creates multiple proccesses like:

user 12:31 /home/user/public_html/website.com/test.php
user 12:31 /home/user/public_html/website.com/test.php
user 12:31 /home/user/public_html/website.com/test.php

How to control this? I need to change the bash script so it can detect if the PHP is being executed.

Is this correct:

#!/bin/bash
while true
if [ "$(pidof test.php)" ]
then
echo "Running"
else
do
 /home/user/public_html/website.com/test.php &
 sleep 5
fi
done

Upvotes: 0

Views: 5235

Answers (6)

sankar muniyappa
sankar muniyappa

Reputation: 356

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&

Upvotes: 2

Robin Hsu
Robin Hsu

Reputation: 4484

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

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 184995

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

Upvotes: 0

Danilo
Danilo

Reputation: 83

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.

Upvotes: 0

Michael D.
Michael D.

Reputation: 1837

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

Upvotes: 0

Ray Burgemeestre
Ray Burgemeestre

Reputation: 1228

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

Upvotes: 2

Related Questions