I have a named pipe being read by a python program, installed as a service in Windows 2008 to print data from a database. The code (php) that writes to the pipe is as follows. ($cmd is the command line passed).
$pipeName = "\\\\.\\pipe\\printerpipe";
#trigger_error("");
#$pipe = fopen($pipeName,'rb+');
$a = error_get_last();
if ($a['message'] == '') {
fwrite($pipe,$cmd);
$ans = fread($pipe,256);
} else {
print error message "service not loaded"
}
There is no custom error handler.
This has worked fine on my test machine (Window7) and on the production server - until today. Today it printed three reports, then reported the service not loaded error, and then printed 4 times more! I doubt that the system was busy or overloaded - I have one user, the report is tiny (most reports are one page), and the printing is spooled. Most reports appear on the printer as the reply to the request is being rendered by the browser.
Also the link between my Python code and the metworked printer appers to break every night, so i have to restart the service every morning. Can someone confirm that this is due to the printer being switched off, or suggest how to re-establish the link, other than restarting the printing service?
I need to make the service reliable, and I don't know how to proceed. Ideas and advice gratefully recieved.
It could be this code - I don't understand the error handling, and the code was copied from a post on the internet.
Related
I am calling filemtime() from a PHP file executed by POST from a JavaScript/HTML app. It returns the same time stamp for a separate test HTML file every two seconds even when I edit the test file with a text editor and I can see its DTM change in the local file system.
If I reload the entire app (Ctrl+F5), the timestamp reported stays the same. At times (once after 4 hours) the time stamp changes, but I don't know what makes this happen.
The PHP part of my code looks like this:
clearstatcache(true,$FileArg);
$R=filemtime($FileArg);
if ($R===false)
echo "error: file not found";
else
echo $R;
This code is called by synchronous Ajax, given only its PHP filename, using setInterval every 2 seconds.
Windows 10 Home, Apache 2.4.33 running locally for HTTP access, PHP 7.0.30 .
ADDED:
The behavior is the same in Firefox, Chrome, Opera, and Edge.
The results are being cached: http://php.net/manual/en/function.filemtime.php
Note: The results of this function are cached. See clearstatcache() for more details.
It almost sounds like Windows is doing some write caching...
stat() on the other hand has an additional note:
Note:
Note that time resolution may differ from one file system to another.
Maybe worth checking stat output.
edit
Maybe it's a bug, or Windows not playing nice, but you could also do a shell_exec with the Windows command showing DTM.
News: it turns out to be an ordinary bug in my app. I copied my Ajax call and forgot to edit it to apply to the test file. So it applied to one of my app files instead and the DTM only got updated when I edited that app file (FTAdjust.js).
When I specify the correct test file, the DTM updates just fine each time I edit it in another process.
It can sometimes be hard to find one's own bug even when it stares one in the face! I kept looking everywhere else but where the mistake was.
Is there a way to delete a thread from Stack Overflow, since it is irrelevant to others?
I'm working in PHP and creating a system with a lot of PHP-driven elements and I have noticed that some of my pages stop displaying text produced using the echo command.
I have made a small example of this. Of course, my program is not supposed to just print allt numbers from 1 to 10000, but this example demonstrates how the script just terminates without any warnings.
Example code:
<?php
for ($i = 1; $i <= 10000; $i++) {
echo $i, '<br>';
}
?>
Output:
1
2
More numbers...
8975
8976
8977
8
What is causing this? Is it a buffer issue, and how do I resolve it?
The fact that your code ran to completion on the cli suggests to me that your script is exceeding the ini.max_execution_time runtime configuration.
Note in the linked documentation that the value of this configuration on the cli is 0 which means it does not time out when run in that environment.
The default setting in the browser is 30 seconds.
You can show your current setting in the browser with:
echo ini_get('max_execution_time');
And you should be able to increase it with:
ini_set('max_execution_time', 0); // turns off timeout
If the script you have shown us behaves as you describe the there's something very wrong going on. If this is a Unix or Linux based system and its repeatedly exhibiting this behaviour then the kernel is terminating the script - unless it has been configured not to do so, the kernel will be forcing a core dump of the process.
Either go build a new system to run your code on or Google how to capture and diagnose a core dump on your operating system.
update
If xdebug is reporting the process is still running then it probably hasn't dumped its core, but "not producing output" != "not running". What state is the process in? What happens when you redirect the ouput? What is the end-to-end output channel when it misbehaves?
The problem did not lie directly with my PHP installation or the application itself, but somewhere in my IDE PHPStorm. When running the code with the same PHP interpreter outside of the IDE's wrappers, it all works fine. The procedures described by the many users here helped with that. Thank you.
I have written a slack auto-message service on heroku that will auto send a message on some event. However, I have accidentally made an infinity loop and it keeps sending error message to my slack account. (the loop caused by retrying on error and I forgot to add a counter on that)
I have tried restarting the server by typing the command heroku restart (at that directory, so app name can be omitted), as well as git pushing my corrected version which should restart the server. I even turn off the server by settings 0 dymo to it. None of these works and I still keep receiving message on slack.
I am quite sure I have turn off my heroku server, so I think there should be other way to stop the process. The php process will have a bash_exec to trigger phantomjs. Not sure if it is related to the current problem. Do anyone has any suggestions?
P.S. As request, this is my code with important information hidden.
<?php
sendSlackMessageToChannel("Request received. Loading, please wait.");
startPhantomJS();
function startPhantomJS() {
$return_value = bash_exec("path/to/phantomjs myscript.js");
sendSlackMessageToMyself($return_value);
if ($return_value == "error")
startPhantomJS();
else
sendSlackMessageToChannel($return_value);
}
I am trying to setup a PHP web application on a Windows Server 2008 SP1 machine. We have this application running correctly on a Windows Server 2003, but are now unable to get the Windows services built with php_win32service PHP extension starting up correctly.
Here are the relevant sections of the PHP code. Here is the section that does the install and uninstall:
if ($argv[1] == 'install') {
win32_create_service(array(
'service' => $service_name,
'display' => $service_name,
'params' => '"'. __FILE__ . '"' . ' run'));
exit;
}
else if ($argv[1] == 'uninstall') {
win32_delete_service($service_name);
exit;
}
else if ($argv[1] != 'run') {
die("Invalid arguments");
}
win32_start_service_ctrl_dispatcher($service_name);
And here is the main loop:
while (1) {
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
}
processQueue();
usleep(500000);
}
The install and uninstall work as long as I run cmd as administrator. If I launch cmd under the account that I use to login to the server then the service is not created win32_create_service. I can also run the script with the run parameter from the command line and it runs correctly, but when I try start the service it just hangs on with the Starting message and never goes into Started state.
I think this issue has something to do with the rights of the LocalSystem account on the machine, but I do not know what rights are needed to get this to work properly. I also do not know how I can debug this and find out what error/issue is occurring with the WIN32_SERVICE_CONTROL_INTERROGATE, especially as I do not have rights on this server to make changes to security settings. Any changes that I need made to security settings I need to communicate to the network administrator so that he can perform the changes. Can anyone offer any help with debugging or resolving this issue?
UPDATE:
This issue only seems to occur in the 64-bit version of PHP. It seems that the 64-bit compile of php_win32service runs into some sort of problem when trying to start the service. I removed the 64-bit versions of PHP and 'php_win32service' and replaced them with the 32-bit versions. The service then started correctly.
Have you tried with NetworkService account? Other way is creating own service account. Do you communicate outside the server? Maybe firewall?
Ps. the network service account has a default password (system known)
Note that this account does not have a password, so any password information that you provide in this call is ignored.
http://msdn.microsoft.com/nl-nl/library/windows/desktop/ms684272(v=vs.85).aspx
Updated Answer:
I am now convinced that this issue arises when you update or downgrade PHP to a different version, but fail to remove and recreate the services using the php_win32service for the newly installed version of PHP. This was mentioned as an aside in my old answer, but after encountering the same thing on another server and resolving it by removing and recreating the service entries, I am convinced that this is the true cause of this issue and that is not a bug in PHP.
Old Answer:
I encountered this issue again when I tried to update PHP on a Windows Server 2008 machine from 5.3 to 5.6. From the testing that I have done I have concluded that php_win32service.dll extension works correctly for PHP 5.3, but not from 5.4 upwards. From 5.4 there seems to be a bug whereby the function win32_get_last_control_message always returns 0 rather than a valid status.
I finally got a clue to what was going on from a post made over here:
http://enlinea.creaelicita.cl/guia/function.win32-set-service-status.html
So, if you have code such as this:
while (1) {
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
}
usleep(3000000);
// Main script goes here
processQueue();
}
then
win32_set_service_status(WIN32_SERVICE_RUNNING);
will never get called, because win32_get_last_control_message never gets the value of 4 (WIN32_SERVICE_CONTROL_INTERROGATE).
A workaround which I use is to add a default case to the switch statement that sets the service status. Like this:
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
default: win32_set_service_status(WIN32_SERVICE_RUNNING);
}
As an aside, I also found that this issue occurred if I upgraded to PHP 5.4 and did not recreate the service entries before trying to start them. So besides applying the above workaround, remember to first uninstall the windows service in question and reinstall it from your PHP script.
Is there a way to view the PHP error logs or Apache error logs in a web browser?
I find it inconvenient to ssh into multiple servers and run a "tail" command to follow the error logs. Is there some tool (preferably open source) that shows me the error logs online (streaming or non-streaming?
Thanks
A simple php code to read log and print:
<?php
exec('tail /var/log/apache2/error.log', $error_logs);
foreach($error_logs as $error_log) {
echo "<br />".$error_log;
}
?>
You can embed error_log php variable in html as per your requirement. The best part is tail command will load the latest errors which wont make too load on your server.
You can change tail to give output as you want
Ex. tail myfile.txt -n 100 // it will give last 100 lines
See What commercial and open source competitors are there to Splunk? and I would recommend https://github.com/tobi/clarity
Simple and easy tool.
Since everyone is suggesting clarity, I would also like to mention tailon. I wrote tailon as a more modern and secure alternative to clarity. It's still in its early stages of development, but the functionality you need is there. You may also use wtee, if you're only interested in following a single log file.
You good make a script that reads the error logs from apache2..
$apache_errorlog = file_get_contents('/var/log/apache2/error.log');
if its not working.. trying to get it with the php functions exec or shell_exec and the command 'cat /var/log/apache2/error.log'
EDIT: If you have multi servers(i quess with webservers on it) you can create a file on the machine, when you make a request to that script(hashed connection) you get the logs from that server
I recommend LogHappens: https://loghappens.com, it allows you to view the error log in web, and this is what it looks like:
LogHappens supports kinds of web server log format, it comes with parses for Apache and CakePHP, and you can write your own.
You can find it here: https://github.com/qijianjun/logHappens
It's open source and free, I forked it and do some work to make it work better in dev env or in public env. That is:
Support token for security, one can't access the site without the token in config.php
Support IP whitelists for security and privacy
Sopport config the interval between ajax requests
Support load static files from local (for local dev env)
I've found this solution https://code.google.com/p/php-tail/
It's working perfectly. I only needed to change the filesize, because I was getting an error first.
56 if($maxLength > $this->maxSizeToLoad) {
57 $maxLength = $this->maxSizeToLoad;
58 // return json_encode(array("size" => $fsize, "data" => array("ERROR: PHPTail attempted to load more (".round(($maxLength / 1048576), 2)."MB) then the maximum size (".round(($this->maxSizeToLoad / 1048576), 2) ."MB) of bytes into memory. You should lower the defaultUpdateTime to prevent this from happening. ")));
59 }
And I've added default size, but it's not needed
125 lastSize = <?php echo filesize($this->log) || 1000; ?>;
I know this question is a bit old, but (along with the lack of good choices) it gave me the idea to create this tiny (open source) web app. https://github.com/ToX82/logHappens. It can be used online, but I'd use an .htpasswd as a basic login system. I hope it helps.