LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2010, 10:53 AM   #1
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Rep: Reputation: 0
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
 
Old 05-31-2010, 11:08 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
try that:
Code:
nohup php -f filename.php
The output of the script will go to the nohup.out file in current directory
 
Old 05-31-2010, 11:15 AM   #3
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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!
 
Old 05-31-2010, 11:18 AM   #4
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Another question:

If I have four scripts to run... can I do nohup for all four?
 
Old 05-31-2010, 11:30 AM   #5
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
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.
 
Old 05-31-2010, 01:26 PM   #6
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
You're a live saver! Thanks!
 
Old 05-31-2010, 02:24 PM   #7
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
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
 
Old 06-04-2010, 03:20 PM   #8
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks James!

One more question though. How do I go back and view the nohup sessions?
 
Old 06-07-2010, 01:29 PM   #9
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Anyone? *bump*
 
Old 06-07-2010, 02:56 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 06-07-2010, 04:40 PM   #11
catmandoman
LQ Newbie
 
Registered: May 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I'll do it with screen then, thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running script in background? bobbyright Linux - Newbie 4 03-02-2010 03:17 AM
Running program in the background HarryBoy Programming 2 06-12-2008 12:42 PM
Running in the background with Linux AlfaRow Linux - Games 1 11-03-2005 04:08 PM
Running functions in the background vexer Programming 6 05-02-2003 02:04 PM
Stuff running in the background...? Shredi Master Linux - Newbie 2 08-22-2001 10:48 AM


All times are GMT -5. The time now is 08:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration