1

I have to run a PHP script as a service. Also it should be run on start up too (when a system crash/rebbot occurs). I tried with a cron job that runs once in a year. Once the cron triggered the process, I commented out it while running the process. But after a system reboot process stopped.

I tried this also but after closing the terminal process got stopped. Also I am not sure it will run on start up or not? Following is my php script.

<?php 
   while (true) {
      $command = "php $cwd\artisan synchronizedb --ptype=2";
      exec($command);
   }
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Kiren S
  • 3,037
  • 7
  • 41
  • 69
  • Possible duplicate of [Run php script as daemon process](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) – Dekel Nov 03 '16 at 11:44
  • @Dekel it wont restart on a system crash. So I want it restart on system startup. – Kiren S Nov 03 '16 at 12:38
  • You may want to [read this about how FreeBSD startup scripts work](https://www.freebsd.org/cgi/man.cgi?query=rc&sektion=8) – Richard Smith Nov 03 '16 at 15:47

1 Answers1

1

FreeBSD has a very nice tool called daemon you could just create a simple script like this

#!/bin/sh

echo "starting XYZ"
daemon -r /path/to/your/script

The -r option, Supervise and restart the program if it has been terminated.

Later you have some options

  1. Add the script to rc.local, edit the file /etc/rc.local

  2. Create a cron that runs only on reboot:

    @reboot  root /path/to/your/script
    
  3. Put your script on /usr/local/etc/rc.d just chmod+x

The cron @reboot could be the easiest one.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
nbari
  • 25,603
  • 10
  • 76
  • 131