4

I'm having a problem where putty gets regularly disconnected. So, when I run a PHP script from the terminal, it always gets interrupted. The script is supposed to run several hours, so I'm not having any luck with it.

How can I completely run this from the server side? I'm reading about cron jobs, but I'm having a hard time understanding at this time. Is there any alternative to cron for what I need?

I have several script PHP files that need to be run, one by one, or perhaps two at a time. Any ideas?

5 Answers 5

9

You don't need to leave it run in a cron job - you can just run the php script inside a screen.

Simply type;

screen php /path/to/myphpscript.php

A screen will continue running even after you disconnect from PuTTY. If you need to check up on it, you can use;

screen -r

To re-attach yourself to this process, and view any output.

Sign up to request clarification or add additional context in comments.

1 Comment

This works well. I did: 1: screen to get in a screen. 2: php my_scripy.php to run the script. 3: from this point on, I can connect or detach from the screen and the script continues until it's finished.
3

You need to prevent the process from terminating when the session disconnects.

Something like this would work:

nohup php myscript.php

4 Comments

Mahmoud, I trying this right now. After I run this command, close putty, and reconnect, I don't see the process when I type ps a. Is that normal?
ps a only shows the current session's processes. Look at top.
This works as well. Thank you! Is there a way to know if the previous session's process is still running in a new session?
I think ps a shows a screen's process in any session. This will works for me.
1

You can create a cron job to start the php script periodically based on a list of time tasks. More info. You could also start the task in the background from the console. i.e. php-cgi script.php& this would make the script a background task

Comments

1

Take a look at GNU Screen; it allows you to detach and reattach a session later, which is perfect for long-running scripts. Cron is a good option if you want it to happen in a recurring fashion; one-off batch jobs can be scheduled with something like at. For more intense computing needs, you might want to look into a more full-fledged job scheduling system like TORQUE.

Comments

0

You can run your program in background

php ./yourscript.php &

1 Comment

Backgrounding a process does not allow it to survive a session termination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.