2

I am trying to setup PhantomJS to run as a service. I found the skeleton file in /etc/init.d and I am running on Ubuntu 12.10. In trying to figure this out I found the symlinks in the /etc/rc*.d directories and found that Apache runs at run levels 2 through 5.

My question is do I just created a symlink called S02phantomjs in the 2 through 5 folders and point it to the script I put in /etc/init.d? Or should I use some application to configure this?

My concern is that I am missing something. And that the order in the number portion of that has a significant importance that I don't want to mess with.

Also I am not sure if there is anything else I have to do to not only make sure this runs on every boot. But get it to start running now.

One last question. My script, based on skeleton, when I run it in terminal it just sits and waits like when I run PhantomJS normally. I would like to get it to just run in the background like Apache does. What do I have to do differently to accomplish this?

Thanks everybody!

Patrick
  • 190
  • 10

2 Answers2

1

If the init script is already there and properly written, it should suffice to enable it normally like any other init script:

update-rc.d phantomjs defaults

or (12.04+)

update-rc.d phantomjs enable

(This is usually done by default when you install packages with apt-get and its cousins...)

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • When I run that I get "update-rc.d: warning: phantomjs start runlevel arguments (none) do not match LSB Default-Start values (2 3 4 5)". I am trying to figure out what that means. – Patrick Jun 14 '13 at 20:49
  • Well, that means it wasn't "properly written" and you should yell at whoever wrote it (or fix it yourself; don't ask me, I don't do upstart scripts). – Michael Hampton Jun 14 '13 at 20:51
  • Lol, ok actually I found out that it works better if you run "update-rc.d phantomjs defaults" first and then run "update-rc.d phantomjs enable". I still get the errors, but now it removes and recreates links. And I admit that I wrote this script. But this is my very first time writing an init script. – Patrick Jun 14 '13 at 20:57
  • I also still need to figure out how to launch this like you would Apache where it launches the service in the background and gives you back a success or failure and then returns you to the prompt. – Patrick Jun 14 '13 at 21:16
  • I wish I could have two selected answers because this helped get it into the proper run levels. – Patrick Jun 17 '13 at 14:03
1

Since PhantomJS isn't going to provide any support to run daemons out of the box, you should take care of your service by yourself. It's great that you have found /etc/init.d/skeleton and used it to create an init script. What you describe shows that your process isn't going into background and this can be fixed by means of some wrapper. This can be start-stop-daemon which is specific to debian-based distributions, or a separate daemon package that can redirect stdin/stdout and handle additional situations.

Since I'm almost sure you use traditional way of writing init script, I suspect you may just add --background to your start-stop-daemon command.

When you're done fixing your script, check you have it owned by root:root with permissions 0755 (e.g. rwxr-xr-x). After this, update sysvinit symlinks with the update-rc.d command provided by Michael Hampton in his answer.

Just as a note, There are some other tools that support sending process to background and may be useful in other cases. For example, supervisor, upstart and systemd support this. Please refer to their documentation to get exact syntax. If you're going to use one of these, writing full init script is not necessary.

Sure thing, init scripts is the oldest and the most supported way of starting daemons.

jollyroger
  • 1,650
  • 11
  • 19
  • I tried running it like this "/etc/init.d/phantomjs start --background" and that didn't push it to the background. I have also tried adding "2>&1>/dev/null &" to the DAEMON_ARGS with no luck. – Patrick Jun 17 '13 at 13:42
  • However, if I add "2>&1>/dev/null &" to the call I make directly from the terminal (so "sudo /etc/init.d/phantomjs start 2>&1>/dev/null &") then it backgrounds it. But I am concerned that this won't happen on boot of the server. – Patrick Jun 17 '13 at 13:43
  • I stand corrected sir. After thinking about it some more I finally understood what you meant. You meant to add "--background" in the init script as one of the parameters for the "start-stop-daemon" call. That call is listed in 5 different places in the skeleton script. So I just added it to the ones that had "--start" and BAM! it works. Thanks!!! – Patrick Jun 17 '13 at 14:01
  • You should have just added it in the `do_start()` function on the second run of the `start-stop-daemon` (the first one just checks if the daemon is already running). Anyway you'd better just try running the whole `start-stop-daemon` command with `--verbose` flags. And you'd better create a PID file as well so you don't kill all daemons with the same name at once. – jollyroger Jun 17 '13 at 14:05
  • Yeah, I figured out the "do_start" thing after posted the comment. My call is set to create a pidfile already. Here is the line "start-stop-daemon --background --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \" – Patrick Jun 17 '13 at 14:12
  • `--pidfile` is actually used to match the actual file to the daemon running. You might want to check if this file is correct after starting your daemon. I suppose you've lost `--make-pidfile` option to `start-stop-daemon`. Please read the relevant manual page, it contains all the necessary info. – jollyroger Jun 17 '13 at 20:38