PHP script modified during parsing/execution - php

I have a PHP script running on a cron that can take a long time to process fully (e.g. 10 minutes). What would happen if this script was modified while it's being parsed? or during execution? The reason I ask is I have a number of scripts across servers that I want to place under version control and i'm concerned about what might happen if the script happens to get updated while it is processing. If it is an issue then I could place some kind of lock on the file while it is running.

Nothing should happen to the running script, because by the time it starts running, PHP would have already parsed it and the opcodes are already in memory, so there's no more disk access.

Related

Make PHP script call itself after some time

I have some limitations with my host and my scripts can't run longer than 2 or 3 seconds. But the time it will take to finish will certainly increase as the database gets larger.
So I thought about making the script stop what it is doing and call itself after 2 seconds, for example.
Firstly I tried using cURL and then I made some attempts with wget. But there is always a problem with waiting for the response and timeouts (with cURL, for example, I just need to ping the script, not wait for a response) or permissions with the server (functions that we use to run wget such as exec seems to be disabled on my server, or something like that).
What do you think is the best idea to make a PHP script ping/call itself?
On Unix/LInux systems I would personally recommend to schedule CRON JOBS to keep running the scripts at certain intervals
May be this SO Link will help you
Php scripts generally don't call other php scripts. It is possible to spawn a background process as illustrated here, but I don't think that's what you're after. If, so you'd be better off using cron as was discussed above.
Calling a function every X amount of seconds with the same script is certainly possible, but this does the opposite of what you want since it would only extend the run time of the script in question.
What you seem to be asking is, contrary to your comment, somewhat paradoxical. A process that calls method() every so often is still a long running process and is subject to the same restrictions as any other process on the server, regardless of the fact that it may be sitting idle for short intervals.
As far as I can see your options are:
Extend the php max_execution_time directive, or have your sysadmin do so if they are willing
Revise your script so that it completes within the time limit
Move to a new server

Long lasting script prevents handling new requests

I have a PHP script on my Apache web server, which starts another several hours running PHP script. Right after the long-lasting script is started no other PHP script requests are handled. The browser just hangs eternally.
The background script crawls other sites and gathers data from ones. Therefore it takes quite long time.
At the same time static pages are got without problems. Also at the same time any PHP script started locally on the server from bash are executed without problems.
CPU and RAM usage are low. In fact it's test server and my requests are only ones being handled.
I tried to decrease Apache processes in order to be able to trace all of them to see where requests are hung. But when I decreased amount of processes to 2 the problem has gone.
I found no errors neither in syslog nor in apache/error.log
What else can I check?
Though I didn't find the reason of Apache hanging I have solved the task in a different way.
I've set a schedule to run a script every 5 minutes. From web script I'm just creating a file with necessary parameters. Script check existence of the file and if it exists it reads its content and deletes to prevent further scheduled start.

What happens if you edit a php script while an instance of it is running?

When you overwrite a php file on a server (say, via SFTP) while it is being processed somewhere (perhaps it's a script that takes several seconds to compelete) does it cancel the currently running script or does that finish out even after the overwrite occurs? I suppose I'm asking: does apache load a php script into memory before executing it (and does it hold on to that in memory for the duration of execution)?
does apache load a php script into memory before executing it (and does it hold on to that in memory for the duration of execution)?
Yes.
Nothing at all. The script has already been loaded into memory in its compiled state - no matter how much time it takes, the web server won't load the new file unless you refresh the page.

Why Scripts Keep Running even if file doesn't exist?

I was running a script in my browser which insert like some 100000 records in database. For some reason I deleted the file in which I have written script but it was little surprising for me that the scripts keeps inserting the records in the database even when the script doesn't exist. Why is that?
The process is executing in memory not from disk.
If you wish to stop a running script, you will need to restart your webserver, or kill the php process. Depending on whether it was running from the command line or not.
When a request is made to the web-server:
The PHP file is loaded into memory, it gets compiled and is running.
Since the file is in memory, any changes to file on disk is not applicable to running file.
If that strikes another question in you, as to "Why the hell it gets loaded into memory ?? Why can't it be executed from the disk directly ?":
Memory here is mostly the RAM, which is faster in doing read/write which is the need to compete with processors speed.
HardDisk is a slow memory and therefore, accessing from it, would make your programs very slow to execute.
To match with processors speed, there comes a need for a executable file to be loaded from Slow Memory (Often HardDisks) to a faster memory (Often Ram, or processor's cache sometimes).
And therefore, the reason and answer, as to why your file on disk is not in sync file in memory.
Also please be assured, that this would be working fine in your next request!!
But if you still want to do it immediately, you can consider stopping/restart your Apache/IIS (whichever applicable) Server, as this will kill the process immediately.
#Umair once you run the script then request go to the server and it'll run on server until process complete . so it doen't matter if file is available or not because it's old request which is under process that's why this happening.

Possible to optimize Php script to limit impact on server memory and cpu?

I have a PHP-script running on my server via a cronjob. The job runs every minute. In the php script i have a loop that executes, then waits one sevond and loops again. Essentially creating a script to run once every second.
Now I'm wondering, if i make the cronjob run only once per hour and have the script still loop for an entire hour or possible an entire day.. Would this have any impact on the servers cpu and or memory and if so, will it be positive or negative?
I spot a design flaw.
You can always have a PHP script permanently running in a loop performing whatever functionality you require, without dependency upon a webserver or clients.
You are obviously checking something with this script, any incites into what? There may be better solutions for you. For example if it is a database consider SQL triggers.
In my opinion it would have a negative impact. since the scripts keeps using resources.
cron is called on a time based scale that is already running on the server.
But cronjob can only run once a minute at most.
Another thing is if the script times out, fails, crashes for whatever reason you end up with not running the script for at max one hour. Would have a positive impact on server load but not what you're looking for i guess? :)
maybe run it every 2 or even 5 minutes to spare server load?
OR maybe change the script so it does not wait but just executes once and calling it from cron job. should have a positive impact on server load.
I think you should change script logic if it is possible.
If tasks your script executes are not periodic but are triggered by some events, the you can use some Message Queue (like Gearman).
Otherwise your solution is OK. Memory leaks can occurs, but in new PHP versions (5.3.x) Garbage Collector is pretty good. Some extensions can lead to memory leaks. Or your application design can lead to hungry memory usage (like Doctrine ORM loaded objects cache).
But you can control script memory usage by tools like monit and restart your script when mempry limit reaches some point or start script again when your script unexpectedly shuts down.

Categories