Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.