0

I have a PHP script which has a forever loop

<?php
    while(1)
        {
             //Do something 
        }
?>

I need to run this script such that, even if I close my SSH session, it should always be running in the background. I know of cronjob, but cronjob will interrupt my script every 1 hour, for example. I want the script to be always running and not at specific times.

Can anyone tell me how to do this? (Its an Apache server running on Ubuntu)

Edit

Thanks to the commnets-

Now I have 3 options -

1) nohup php filename.php &

2) screen php filename.php

3) Doing something like this using cronjob http://reviewsignal.com/blog/2013/08/22/long-running-processes-in-php/

Can anyone tell me which is a better option? I want such that, even if an error occurs or it may be killed, or it may crash for some reason, the script should restart itself.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
SMG
  • 95
  • 1
  • 2
  • 11
  • 1
    You run it from the command line or as a service. `/usr/bin/php -f myscript.php` for example. – Jay Blanchard Nov 18 '16 at 15:15
  • 1
    Performing cronjob every minute seems a better solution. While maximum execution time for cronjob by default is 180 seconds, which is still greater than 1 minute, so technically speaking, its running continuously. – Manikiran Nov 18 '16 at 15:20
  • @JayBlanchard Will this run my script even I close the SSH terminal? – SMG Nov 18 '16 at 15:22
  • @Manikiran I do not want to interrupt my script. If I use cronJob, it will start my script ever minute, won't it? – SMG Nov 18 '16 at 15:23
  • No it wont work for you – Blueblazer172 Nov 18 '16 at 15:25
  • @Blueblazer172 What wont work ? – SMG Nov 18 '16 at 15:25
  • Running the script when you delete your ssh session. It only works if you use cronjobs – Blueblazer172 Nov 18 '16 at 15:26
  • 2
    You start something from the command line and it will continue to run even after you exit your SSH session. You can also use [Upstart](http://upstart.ubuntu.com/) to run your item as a service which is the best way to go on Linux systems. – Jay Blanchard Nov 18 '16 at 15:26
  • 3
    You should look into something like supervisord: That can start your process and restart it if something happens to it (code problem / exception, server reboot, etc.) – jeroen Nov 18 '16 at 15:27
  • 2
    @Blueblazer172 you can start a job from the command line in the background and it will continue to run after you exit the SSH session. – Jay Blanchard Nov 18 '16 at 15:27
  • `nohup php script.php &`, but you should check if the script is running, as it may be killed, or it may crash for some reason. And this is actually a difficult problem. So there are too many possible answers. – Ruslan Osmanov Nov 18 '16 at 15:28
  • http://reviewsignal.com/blog/2013/08/22/long-running-processes-in-php/ How about this solution? – SMG Nov 18 '16 at 15:36
  • You want to *daemonize* your script, for which there are many possibilities. Look at the (new) duplicate. – deceze Nov 18 '16 at 15:52
  • @deceze as Ruslan says in the comments - `nohup php script.php &, but you should check if the script is running, as it may be killed, or it may crash for some reason` using nohup I'll have to check myself if the script is running. Which I dont want to.. – SMG Nov 18 '16 at 15:55
  • 1
    Read more than just the first answer. There are a ton of methods listed to create a startup service from any command line program. – deceze Nov 18 '16 at 15:57
  • @deceze I only read `nohup php filename.php &` as all the answers – SMG Nov 18 '16 at 16:18

0 Answers0