Linux – How to set a binary to init on boot like Apache does (run as a service)

bootinit.dlinuxUbuntuupstart

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!

Best Answer

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.