Running php in the background
Hello all!
I am new to linux so bear with me :) I have a LAMP server with some php files. When I do this: > php -f filename.php It works great. But of course, it stops when I close the SSH window. I need to be able to run it and leave it running. The script is a crawler and it takes about 3 hours to complete it. So I tried this: > php -f filename.php & This doesn't work at all. It doesn't even execute the script. I will appreciate any help you can provide. Regards |
try that:
Code:
nohup php -f filename.php |
Hi Agrouf,
This seems to work! So the script is going to execute till its completed if I write it out to a file? Thanks for your help! |
Another question:
If I have four scripts to run... can I do nohup for all four? |
The nohup command detaches the script from the session so you can quit your ssh session and it will continue to run. It will continue to run until it ends or is explicitly killed. It will not end when you log off.
You can run as many scripts as you want with the nohup command. If you use the nohup command while another script is already running, both scripts will run in parallel. If they are both resource hungry, they may slow the machine down a bit or a lot. Be careful if your scripts depend on the same resource as they may block each other (for instance one script reading a file while the other is writing to it: it may cause some problems.) If your scripts are not resource intensive and don't depend on the same resource there should be no problem running 4 of them or more. You can always renice a script running in the background to give it a lower priority if you think it should not steal resources from other processes. |
You're a live saver! Thanks!
|
Another thing people sometimes do is run it in a screen session. A quick primer:
Code:
ssh user@blah Code:
screen -r Its a slightly more wordy/meaty way to do it, but it allows you to keep this session going across connections/ login and logouts, so you may find it useful in other contexts |
Thanks James!
One more question though. How do I go back and view the nohup sessions? |
Anyone? *bump*
|
You can't - not after the originating shell has been killed.
That's where screen comes in handy - you just run the command w/o nohup, and you can get back to it anytime you wish. Cheers, Tink |
I'll do it with screen then, thanks!
|
All times are GMT -5. The time now is 03:26 AM. |