14

I occasionally use forever.js for quick and dirty deploying of CLI type Node.js applications to production environments where I don't want a full on supervisord deployment.

I was wondering if there was an equivalent for PHP? At the moment we have a queue processing system that get's messages from SQS and processes them synchronously into a database (it can't be done async as that causes all sorts of nasty row locking issues in this particular use case.) At the moment it runs ever minute using cron, but it often finishes early and I want it to start running again. I can't have more than one process running at a time.

Any *nix command/software/bash type ideas are welcome.

2
  • 6
    Why not use forever.js to start your PHP script? Aug 14, 2013 at 7:55
  • doh! didn't think about that will give it a go. Aug 14, 2013 at 9:03

2 Answers 2

30

Just use forever with your php script (use the -c argument to instruct forever to use php):

$ cat test.php
<?php
sleep(3);
print("foobar\n");
exit;
?>
$ forever -c php test.php
foobar
error: Forever detected script was killed by signal: null
error: Forever restarting script for 1 time
foobar
error: Forever detected script was killed by signal: null
error: Forever restarting script for 2 time
...
6
  • Dude, I had no idea you could do this. This fixes my problem. Thanks! Aug 14, 2013 at 9:16
  • 2
    holy crap really?? thats awesome! Mar 10, 2015 at 23:05
  • 2
    By the way, it turned out that forever -c is not putting the script at the background, i have to use screen to do that, something like screen forever -c php myscript.php and detach it to run "forever"! Mar 30, 2015 at 21:37
  • 6
    @Ma'moonAl-Akash screen was not meant for this, nohup was. Anyways, to run a script with forever as a daemon process use start action, like so: forever start -c php mysqcript.php. Make sure to specify log files for forever instance and script's STDOUT / STDERR, like so: forever start -a -l /path/to/myscript_forever.log -o /path/to/myscript_out.log -e /path/to/myscript_err.log -c php myscript.php Jan 11, 2016 at 15:28
  • 1
    @SergeyLukin Thanks, You're 100% correct, please do NOT upvote my comment, Sergey's comment is the correct one, In fact i think it is the best answer to the question, I am not going to delete my comment in order to show the others where i have been mistaken! Jan 11, 2016 at 22:16
2

I've just discovered this tool supervisord and it works great. I've used this ratchet tutorial to make it run in minutes !

If I remember correctly forever script do not restart when you reboot server ?

1
  • We've been using supervisord for about 8 months now and it's great, we actually use in combo with foreverd. However because nearly all our stuff is moving over to docker we've actually found ourselves using docker's built in daemonizing tools eg: -d --restart=always May 27, 2015 at 12:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.