laketuna
laketuna

Reputation: 4090

Running a PHP script completely on server side

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?

Upvotes: 4

Views: 3042

Answers (5)

Eoghan
Eoghan

Reputation: 1761

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.

Upvotes: 9

J.L.
J.L.

Reputation: 39

You can run your program in background

php ./yourscript.php &

Upvotes: 0

Wyatt Anderson
Wyatt Anderson

Reputation: 9903

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.

Upvotes: 1

secretformula
secretformula

Reputation: 6432

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

Upvotes: 1

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29579

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

Something like this would work:

nohup php myscript.php

Upvotes: 3

Related Questions