Run a PHP Script Every Second Using Cli

Run a PHP script every second using CLI

You could actually do it in PHP. Write one program which will run for 59 seconds, doing your checks every second, and then terminates. Combine this with a cron job which runs that process every minute and hey presto.

One approach is this:

set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
doMyThings();
sleep(1);
}

The only thing you'd probably have to watch out for is the running time of your doMyThings() functions. Even if that's a fraction of a second, then over 60 iterations, that could add up to cause some problems. If you're running PHP >= 5.1 (or >= 5.3 on Windows) then you could use time_sleep_until()

$start = microtime(true);
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
doMyThings();
time_sleep_until($start + $i + 1);
}

How do i run a php script every second?

I searched for a better solution but it seems that my first idea, with clean code, is the best solution.

This is my current code:

<?php
set_time_limit(60);
$start = time();

for ($i = 0; $i < 59; ++$i) {
// Do whatever you want here
time_sleep_until($start + $i + 1);
}
?>

Run a PHP script every second for all users

What you are trying to do is not functional for a request/response way with PHP.

You should consider other options, either have a PHP CLI application running on the server side, or use other option for your "game server" like NodeJS, and also consider websockets for the communication instead of normal GET/POST requests.

Websockets allows you to have a constant communication both ways between the client and the server, and then an actual server side application would allow you to keep those communications going and act on them.

Run php script from command line endless

nohup php myscript.php &

the & puts your process in the background.
The solution from Run php script as daemon process
To kill it:

1) display all running proceses with: ps aux | less or top command

2) find pid(process id) and kill with: kill pid

How to run a PHP script continuously?

For this particular issue cron jobs have been invented. Cron jobs are timed jobs that can for example execute a PHP script.

You could set up a cron job to check which user should receive his/her sms every hour. Depending on your operating system you can set up these cron jobs. For linux distrubutions there are tons of guides on how to set this up.

From Wikipedia:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos). (Ken Thompson, author of cron, has confirmed this in a private communication with Brian Kernighan.)

I have added a resource explaining how to use cron jobs.

An alternative method is to keep a PHP script running in the background:

// Keep executing even if you close your browser
ignore_user_abort(true);

// Execute for an unlimited timespan
set_time_limit(0);

// Loop infinitely
// If you create a file called stop.txt,
// The script will stop executing
while (!file_exists('stop.txt')) {
// Retrieve user data and sens sms messages

// Wait for an hour
sleep(3600);
}

Update

ignore_user_abort(true);
set_time_limit(0);
$data = file_get_contents('filename.txt');
while (!file_exists('stop.txt')) {
// Add 1 to $data
$data = $data+1;
// Update file
file_put_contents('filename.txt', $data);

// Wait 4 seconds
sleep(4);
}

To stop executing create a file called stop.txt

Resources

  • About Cron jobs

how to run my PHP script every 20 minutes

Use Putty to connect to your server via SSH. It will give you a command line interface (CLI).

Once in the CLI, you need to type the following command:

crontab -e

This will open the CRON config file. Then you need to add a line at the bottom of the file:

*/20 * * * * /usr/bin/php /path.php

Finally, you save and quit using CTRL+X

And that's it. The PHP script will be executed every 20 minutes. Of course, I'm assuming you're SSH access have the right permissions.

Now I don't know if your script path.php is already on your server, but this is where you need FileZilla, to upload the file on the server, or to update it every time you change it's content.



Related Topics



Leave a reply



Submit