Run php script continuously in background

put in simple words: i am writing php scripts which send and receive sms, scripts will calculate to send users campaign SMS every week based on each user registration date, for example every monday 10 AM send sms to mr. A and every friday at 7 pm sends sms to miss B.. and php scripts will take care of everything needed ..

problem : obviously a very funny way is to have someone refresh the main page of my application every some seconds or so to be able to continue to calculate and understand what and when to do jobs, or have the main page always open on my computer so javascripts and jquery will handle the rest!

My Question : how can i have my php program or scripts to be something like awake without need to someone refreshes or have open the main page? by awake i mean like it senses the next schadule and executes it and so on ..

some raw ideas to answer : perhaps i could call the main page using ajax or curl every 10 seconds .. but i don't know how to awake ajax or curl in first place ..

i see some internet posts suggest something like command line either in linux unix or windows .. but i usually access the host not the command line is it right ? or command line is something in host and i don't know it, if so please help me ..

important example : there are php wp plugins like total cache and supper cache which seem to be always on and awake of schedules without need of somebody refreshing a page ..

please give answers all in php and php families if possible, i don't know unix or those kind of programmings at all ..

------- accourding to answers made some progress to question .. now i have this bellow script :

ignore_user_abort(true);
set_time_limit(0);
$data = file_get_contents('filename.txt');
$data = $data+1;
file_put_contents('filename.txt', $data);
$page = $_SERVER['PHP_SELF'];
$sec = "4";
header("Refresh: $sec; url=$page");

it works! even when i restart the local host . main problem is now when i closed the main page it stopped incrementing in filename.txt and when reoppend the page two instance where running the increment continued so : should'nt it continue to increment even when i close the page ? and how i stop it ? and is it normal to have more than one instance of the page run in background?

finally : according to instructions on this page it's best i create a starter or reload page then use commands to initiate this reload page for example every 1 minute and then write PHPs like normal ..

last not least : how to stop this background script ? for update or maintenance ..

Amine Dev ・ Jan 15th, 2019 Views

How to run a php script in background

When we need to run some script without waiting the fronted user till the process is not completed, For that we need to execute some script in background to hiding the execution time to user.This process is hidden to the end user. It improves your Website efficiency.

The concept, In LINUX there is a shell script which is used to run the process in background. You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running.


Syntax : {command} &

Example: ls -l & exec php index.php > /dev/null 2>&1 & echo $

How to check the background process in Linux?

ps -l (list all process)
ps -ef (all full details of process)

What is the command to execute a php script in background?

Syntax: nohup exec arg1 arg2 > /dev/null &

Example: nohup exec php process.php hello world > /dev/null &

What is nohup?

Most of the time you log-in into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell.

What do you do to keep job running in the background when process gets SIGHUP?

Run php script continuously in background
Checkout our latest product - the ultimate tailwindcss page creator 🚀

What is exec?

This command is used to execute a process in Linux. It can process one or more process at a time.

How to use this PHP library on your code?

Step 1: create two file name index.php and process.php

step 2: include the PHPBackgroundProcesser.php file in the index.php

step 3: create a instance of the class BackgroundProcess

We can use this:

Type 1:

$proc=new BackgroundProcess('exec php <BASE_PATH>/process.php hello world');

Type 2:

$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world');
$proc->start();

Type 3:

$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world')->start();

Alternatively you can execute PHP URL in background with out the direct (.php) file.

$process=new BackgroundProcess("curl -s -o <Base Path>/log/log_storewav.log <PHP URL to execute> -d param_key=<Param_value>");

How to get all process which is running?

$proc=new BackgroundProcess();
print_r($proc->showAllPocess());

How to kill a process ?

$proc=new BackgroundProcess();
$proc->setProcessId(101)->stop(); //set the process id.

source    author :Sanjay Panda

How do I run a PHP script continuously?

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.

Can PHP run in the background?

So, to run any background process from PHP, we can simply use either exec or shell_exec function to execute any terminal command and in that command, we can simply add & at the last so that, the process can run in the background.

Can a PHP script run forever?

You can make it run forever by either setting the value or call set_time_limit in your script (http://php.net/manual/en/function.set-time-limit.php).