Rectangle 27 10

  • create a batch file to run your php script using php executable "C:\wamp\php\php.exe C:\wamp\www\index.php"
  • add this batch file in Scheduled Task in Windows control panel.

hi, thanks. I just create two bat files as you said and it works.

windows - Run a php script as a background process in wamp server - St...

php windows wamp background-process
Rectangle 27 66

Doing some experimentation with exec and shell_exec I have uncovered a solution that worked perfectly! I choose to use shell_exec so I can log every notification process that happens (or doesn't). (shell_exec returns as a string and this was easier than using exec, assigning the output to a variable and then opening a file to write to.)

I'm using the following line to invoke the email script:

shell_exec("/path/to/php /path/to/send_notifications.php '".$post_id."' 'alert' >> /path/to/alert_log/paging.log &");

It is important to notice the & at the end of the command (as pointed out by @netcoder). This UNIX command runs a process in the background.

The extra variables surrounded in single quotes after the path to the script are set as $_SERVER['argv'] variables that I can call within my script.

The email script then outputs to my log file using the >> and will output something like this:

[2011-01-07 11:01:26] Alert Notifications Sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 38.71 seconds)
[2011-01-07 11:01:34] CRITICAL ERROR: Alert Notifications NOT sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 23.12 seconds)

What is that $post_id after the path of the php script?

@Perocat it is a variable that you are sending to the script. In this case it is sending an ID which will be a parameter to the script being called.

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 2

The more simpler way to run a php script in background is php script.php >/null/dev &

The php will run in background and the page will also reach to action page faster..

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 2

If you can access the server over ssh and can run your own scripts you can make a simple fifo server using php (although you will have to recompile php with posix support for fork).

The server can be written in anything really, you probably can easily do it in python.

Or the simplest solution would be sending an HttpRequest and not reading the return data but the server might destroy the script before it finish processing.

<?php
define('FIFO_PATH', '/home/user/input.queue');
if(!file_exists(FIFO_PATH)) {
    die(FIFO_PATH . ' doesn\'t exist, please make sure the fifo server is running.' . "\n");
}

function postToQueue($data) {
    $fp = fopen(FIFO_PATH, 'w+');
    stream_set_blocking($fp, false); //don't block
    $data = json_encode($data) . "\n";
    if(fwrite($fp, $data) != strlen($data)) {
        echo "Couldn't the server might be dead or there's a bug somewhere\n";
    }
    fclose($fp);
}
$i = 1000;
while(--$i) {
    postToQueue(array('xx'=>21, 'yy' => array(1,2,3)));
}
?>

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 15

On Linux/Unix servers, you can execute a job in the background by using proc_open:

$descriptorspec = array(
   array('pipe', 'r'),               // stdin
   array('file', 'myfile.txt', 'a'), // stdout
   array('pipe', 'w'),               // stderr
);

$proc = proc_open('php email_script.php &', $descriptorspec, $pipes);

The & being the important bit here. The script will continue even if the original script has ended.

This works when I do not call proc_close when the task has been completed. proc_close makes the script hang about 15 to 25 seconds. I would need to keep a log using the pipe information, so I have to open a file to be written to, close it, and then close the proc connection. So, unfortunately, this solution does not work for me.

Of course you don't call proc_close, that's the point! And yes, you can pipe to a file with proc_open. See updated answer.

@netcoder : what if I don't want to store the result in any file. what changes are required in stdout ?

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 11

It could be that you're the one who does not understand how PHP works. PHP is a full language interpreter, and it's completely possible to run PHP scripts without a browser, outside of a Web server: On the command line or in an IDE or other GUI environment.

The PHP preprocessor of which you speak is only the function of an Apache module that calls on the PHP interpreter for this particular limited purpose.

Most of the questions I relates to it in the use with a web server. I specified that in the question.

While this is true, the context which 99.9% of all PHP-discussions evolve around is server-side scripting. Server-side scripting is, after all, what PHP was designed for. Assuming that PHP == server-side scripting by default is very practical.

I agree, the preprocessor part is somewhat anachronistic at this point and mainly is in the name to make the acronym work.

Why PHP script is not workig in a web browser? - Stack Overflow

php
Rectangle 27 5

exec("php script.php")

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

So if you redirect the output to a log file (what is a good idea anyways), your calling script will not hang and your email script will run in bg.

Thanks, but this did not work. The script still "hangs" while it processes the second file.

Look at the commend php.net/manual/en/function.exec.php#80582 Seems as this is because Safe_mode is enabled. There is a workaround under unix.

Unfortunately, safe mode is not enabled on our installation. It's odd that it is still hanging.

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 3

Node.js cannot run PHP out of the box. It is not a PHP server, and cannot parse PHP code. You might be able to use a JavaScript PHP shim, but these will likely not have reliable performance.

One such option is php.js, which is a JavaScript PHP VM, but even still, I do not recommend this.

My wholehearted recommendation is that you convert your code to Node.js if you're going to use Node.js at all. Your code is relatively trivial to convert as well. See Node.js' built in fs API. In particular, the fs.readFile() function which you are already using to read the PHP file.

If you need to convert each new line into an HTML page break, that's also relatively trivial. First, you'll want to coerce the data to a string using the toString() method. You can also use Regex matching, or use split() and join() to convert newlines into </br>. For this example, I'll use the split()/join() method.

fs.readFile('filename.txt', 'utf-8', function(err, data) {
    var str = data.toString();
    str = str.split(/\r\n|\r|\n/g).join('</br>');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(str);
    res.end();
});

How to run PHP Script in Node.js? - Stack Overflow

php node.js
Rectangle 27 11

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

nohup php myscript.php &

There is however a very serious problem. As you said PHP's memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php
nohup php myscript.php &

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() {
  GLOBAL $lock;
  mysql_close();
  fclose($lock)
  exit();
}
pcntl_signal(SIGINT, 'clean_up');

The idea behind the $lock is that the PHP script can open a file with a fopen("file","w");. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

linux - Run php script as daemon process - Stack Overflow

php linux unix daemon
Rectangle 27 11

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

nohup php myscript.php &

There is however a very serious problem. As you said PHP's memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php
nohup php myscript.php &

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() {
  GLOBAL $lock;
  mysql_close();
  fclose($lock)
  exit();
}
pcntl_signal(SIGINT, 'clean_up');

The idea behind the $lock is that the PHP script can open a file with a fopen("file","w");. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

linux - Run php script as daemon process - Stack Overflow

php linux unix daemon
Rectangle 27 11

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

nohup php myscript.php &

There is however a very serious problem. As you said PHP's memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php
nohup php myscript.php &

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() {
  GLOBAL $lock;
  mysql_close();
  fclose($lock)
  exit();
}
pcntl_signal(SIGINT, 'clean_up');

The idea behind the $lock is that the PHP script can open a file with a fopen("file","w");. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

linux - Run php script as daemon process - Stack Overflow

php linux unix daemon
Rectangle 27 11

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

nohup php myscript.php &

There is however a very serious problem. As you said PHP's memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php
nohup php myscript.php &

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() {
  GLOBAL $lock;
  mysql_close();
  fclose($lock)
  exit();
}
pcntl_signal(SIGINT, 'clean_up');

The idea behind the $lock is that the PHP script can open a file with a fopen("file","w");. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

linux - Run php script as daemon process - Stack Overflow

php linux unix daemon
Rectangle 27 11

I do not know the specifics but perhaps there is another way to trigger the PHP process. For instance if you need the code to run based on events in a SQL database you could setup a trigger to execute your script. This is really easy to do under PostgreSQL: http://www.postgresql.org/docs/current/static/external-pl.html .

Honestly I think your best bet is to create a Damon process using nohup. nohup allows the command to continue to execute even after the user has logged out:

nohup php myscript.php &

There is however a very serious problem. As you said PHP's memory manager is complete garbage, it was built with the assumption that a script is only executing for a few seconds and then exists. Your PHP script will start to use GIGABYTES of memory after only a few days. You MUST ALSO create a cron script that runs every 12 or maybe 24 hours that kills and re-spawns your php script like this:

killall -3 php
nohup php myscript.php &

But what if the script was in the middle of a job? Well kill -3 is an interrupt, its the same as doing a ctrl+c on the CLI. Your php script can catch this interrupt and exit gracefully using the PHP pcntl library: http://php.oregonstate.edu/manual/en/function.pcntl-signal.php

function clean_up() {
  GLOBAL $lock;
  mysql_close();
  fclose($lock)
  exit();
}
pcntl_signal(SIGINT, 'clean_up');

The idea behind the $lock is that the PHP script can open a file with a fopen("file","w");. Only one process can have a write lock on a file, so using this you can make sure that only one copy of your PHP script is running.

linux - Run php script as daemon process - Stack Overflow

php linux unix daemon
Rectangle 27 12

I had the same issue - wanting to check if a script is running. So I came up with this and I run it as a cron job. It grabs the running processes as an array and cycles though each line and checks for the file name. Seems to work fine. Replace #user# with your script user.

exec("ps -U #user# -u #user# u", $output, $result);
foreach ($output AS $line) if(strpos($line, "test.php")) echo "found";

process - How to check if a php script is still running - Stack Overfl...

php process queue monitoring
Rectangle 27 4

And why not making a HTTP Request on the script and ignoring the response ?

If you make your request on the script you need to call your webserver will run it in background and you can (in your main script) show a message telling the user that the script is running.

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 16

I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.

You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.

nohup php myscript.php >myscript.output 2>&1 &

This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.

The final & causes the script to run in the background so you can do other things whilst it is running or logout.

debian - How to run a php script through the command line (and keep it...

php debian putty remote-server
Rectangle 27 111

How do I pass a PHP array to Javascript?

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

PHP and Javascript can only communicate via strings. You can pass the string "foo" very easily to Javascript. You can pass the number 1 very easily to Javascript. You can pass the boolean values true and false easily to Javascript. But how do you pass this array to Javascript?

Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )

The answer is serialization. In case of PHP/Javascript, JSON is actually the better serialization format:

{ 1 : 'elem 1', 2 : 'elem 2', 3 : 'elem 3' }

Javascript can easily reverse this into an actual Javascript array.

This is just as valid a representation of the same data structure though:

a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}

But pretty much only PHP uses it, there's little support for this format anywhere else. This is very common and well supported as well though:

<array>
    <element key='1'>elem 1</element>
    <element key='2'>elem 2</element>
    <element key='3'>elem 3</element>
</array>

There are many situations where you need to pass complex data structures around as strings. Serialization, representing arbitrary data structures as strings, solves how to do this.

what is the explanation of those a,i,s etc in a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";} ? And if u don't mind , an example of serializing the array(might not be relevant to the theme of this post ) to send it to js.

As far as I'm aware there's hardly a formal specification of that format to be found, but you can guess, can't you? i:1 = integer 1, s:6:"elem 1" = string with 6 characters "elem 1"... And what example are you asking for, I thought I gave one?

" But how do you pass this array to Javascript? Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 ) "... just the exact code snippet for it

How to use php serialize() and unserialize() - Stack Overflow

php
Rectangle 27 2

The more simpler way to run a php script in background is php script.php >/null/dev &

The php will run in background and the page will also reach to action page faster..

scripting - How can I run a PHP script in the background after a form ...

php scripting background
Rectangle 27 21

In summary, GitHub will POST to a supplied URL when someone pushes to the repo. Just write a short PHP script to run on your linode VPS and pull from GitHub when it receives said POST.

This sounds great, but how do you "pull from github" from PHP?

I assume some library exists, but in the worst case, more use of exec than ideal

If I am not mistaken, in order for you to "pull from github" from yuor server, you need to install git on your server. I hope I am right

git - Automatically Deploy From GitHub To Server On Push - Stack Overf...

git deployment github
Rectangle 27 9

nohup php myScript.php &

Be aware though, that the script won't automatically restart after terminating.

debian - How to run a php script through the command line (and keep it...

php debian putty remote-server