How to stop a php script from command prompt?

I am running a PHP script and it takes a long time to execute (parsing huge CSV file).

How can I stop the script?

It seems from command prompt you can run:
> kill $pid

But I do not know how to find out the pid…

how do you run the php script? from command line or through a web page request?

sorry, it is run through the browser.

you could use the ps command on linux to find the pid.
maybe take a look at pkill, or combine ps with grep

edit, oh it through the browser. unless php is run as a cgi, I dont think that will work.

Yep, can’t kill the script unless it was executed as cgi process or from command line. On the other hand, you can run the csv parsing script as a background process, see this : http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

Actually running from command prompt (Im on Win2003) would be preferable, however in the past I found that I got permission problems when doing that. I was unable to write to files using PHP, even though it worked fine when run through the browser.

I tried to give the write permissions on the file to php-cgi but couldn’t figure out how to do it.

You sure you had permission problems, and not filepath problems? Keep in mind when running php through its CLI, relative filepaths may not work like you think.

I dont remember but I just know that it stopped working when it came to writing to a file.

Why would relative paths stop working?

Because relative filepaths are relative to php’s current working directory. The current working directory is not the same directory as where the script lives when run via the cli, unlike the typical webserver enviornment.

You can use absolute filepaths, or chdir()

http://www.php.net/features.commandline

thanks i will try to set that up.

When PHP run through Apache, the unix user is that of Apache. Eg. www or something similar. When you run a script from the command line, it is run as the current user. On unix systems, files have three types of permissions; user, group and global. Each of these roles can have read, write or execute rights. If you create a file under one user and set the permissions so that other users can access them, well then other users can’t access them. You can solve it by putting both users in the same group, set the group ownership to this for the files that need to be shared and finally set group permissions to the same as user permissions (read+write for files and read+write+execute for directories).

Oh, and if you have open_basedir or safe_mode turned on for PHP, things gets a bit more complicated.