LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running php in the background (https://www.linuxquestions.org/questions/linux-newbie-8/running-php-in-the-background-811283/)

catmandoman 05-31-2010 09:53 AM

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

Agrouf 05-31-2010 10:08 AM

try that:
Code:

nohup php -f filename.php
The output of the script will go to the nohup.out file in current directory

catmandoman 05-31-2010 10:15 AM

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!

catmandoman 05-31-2010 10:18 AM

Another question:

If I have four scripts to run... can I do nohup for all four?

Agrouf 05-31-2010 10:30 AM

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.

catmandoman 05-31-2010 12:26 PM

You're a live saver! Thanks!

jamescondron 05-31-2010 01:24 PM

Another thing people sometimes do is run it in a screen session. A quick primer:

Code:

ssh user@blah
user@blah's password:
{SSH}user@blah:~$ screen
# Screen started
{SSH}user@blah:~$ php -f filename.php
# Ctrl+A and D
[detached]
{SSH}user@blah:~$ logout
Connection to blah closed.

This allows you to keep this running. Then to go back to it, ssh back in and use
Code:

screen -r
to re-attach that session.

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

catmandoman 06-04-2010 02:20 PM

Thanks James!

One more question though. How do I go back and view the nohup sessions?

catmandoman 06-07-2010 12:29 PM

Anyone? *bump*

Tinkster 06-07-2010 01:56 PM

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

catmandoman 06-07-2010 03:40 PM

I'll do it with screen then, thanks!


All times are GMT -5. The time now is 03:26 AM.