tags:

views:

2794

answers:

5

I have a rather long php script and whenever my internet connection skips out for a second the browser seems to stop the script. I can't sit around for 8 hours for my script to run so I figured I could just run it via ssh and be come back the next day and get my output file. However simple typing the scripts name into ssh doesn't seem to work. is there a special command to run php scripts through ssh?

+3  A: 

In general, you should be able to run the script from the command-line like this

php myscript.php

Doing it on a remote host via ssh could be done like this

ssh [email protected] "php myscript.php"
Mark Biek
And any redirects needed.
strager
+1  A: 

try

php script.php.

If that doesn't work, you would have to locate the php executable and then execute it.

Btw, you can use screen, so if the connection to the computer is lost, the script still runs.

Ikke
what do you mean by use screen? I've never heard of that
I've explained screen in my post :D
Mez
i asumed you use linux. Screen is a program that keeps programs running even when the shell is closed. You can easily restore the session so the script is not aborted. See http://www.gnu.org/software/screen/
Ikke
Thanks I'll have to look into it :)
A: 

Alternatively you can just call ignore_user_abort() and close your browser after you hit the page.

Greg
except this wont necessarily work, and PHP may well just stop on it's timeout.
Mez
Actually it will work. If the PHP time limit comes into play you can use set_time_limit
Greg
Doesn't ignore_user_abort only work when php runs as an apache module?
troelskn
+6  A: 

If your internet connection is dropping out, then this is probably going to be a problem across SSH as well, and well, having an SSH window open isn't always the best thing to do (what happens if you accidentally close the ssh window?)

I would suggest SSHing into the server, then running a program called "screen", which will keep on running whatever you run inside of it, even if your connection drops.

To do this, first ssh into the server, and type

screen

This will load up screen, hit enter to bypass the welcome screen

Now, run your PHP script

php /path/to/your/php/script.php

this will start PHP running,

You can now close the window if you want, and the script will keep on running

To get back to your screen session, connect to the server, and run the command

screen -raAD

which will reconnect you to your session, as if you'd had the window open all the time.

This is actually pretty good for running long winded scripts, or even for running a console based IRC session :D

Mez
+1  A: 

In addition to what everyone else said, you'll probably want to use nohup as well.

ssh user@host "nohup php script.php"

That way it'll keep running even if your ssh connection drops. You could also use screen in place of nohup if you want.

Ant P.