I've got a 3-rd party proprietary application server daemon which can be started and stopped by couple of command lines. I need this daemon to start when the system starts up and correctly stopped on system shutting down. How do I correctly implement this? Is it enough to copy some script inside /etc/init.d and modify it accordingly?
init.d is the old, deprecated system for starting daemons; is has been supplanted by upstart. Upstart has the advantage of being far easier to configure and allows proper sequencing of task initialization.
The configuration files for upstart live in /etc/init and if your daemon has no pre-requisites it can be as simple as tty1.conf:
in which case you can copy that file and modify to taste. More complex configurations are best documented at the upstart site and in other entries in /etc/init.
added in response to comment
Whether you use upstart or init.d, you'll still need some way of determining when Firebird is properly initialized. Unfortunately, Firebird itself doesn't seem to have a good way of verifying that it is installed and running. Therefore, the recommendation to stick your program start into /etc/rc.local is certainly the easiest, and on Ubuntu - at least - is guaranteed to run about as late as possible in the boot process.
if you do not want to migrate to UPSTART, but want the classic approach, you must:
NOTE: i am saving the service and the program with the same name in different directories (but you can change this, as long as it is reflected in your service file). change "myscriptname" and "myprogramname" to real names!
save your program that will run as a service in /usr/sbin
sudo cp myprogramname /usr/sbin/myscriptname
create a basic startup script (use /etc/init.d/skeleton as reference)
move this script to /etc/init.d
sudo mv /etc/init.d/myscriptname
give this script executable permission (i used 775, but you can set it lower)
sudo chmod 755 /etc/init.d/myscriptname
goto /etc/init.d
cd /etc/init.d
include in startup list with low startup priority
sudo update-rc.d myscriptname defaults 97 03
reboot your machine and check if the service has started properly
if your service is not starting properly, you should first check if it runs when called by hand:
below i include a sample service file that actually works. compare it to skeleton service so as to understand what you need to configure. NOTE: this works on Ubuntu 12.04 amazon cloud AWS EC2 classic LAMP implementation (also on Kubuntu 15.10).
Make a copy of /etc/init.d/skeleton and edit it at appropriate places to start/stop/restart your service. It is very well commented so you should be able to create a working init.d script in no time.
/etc/rc.local
2020 April
Some good answers here. For up-to-date instruction:
init.d
services were replaced byupstart
(which uses/etc/init
instead of/etc/init.d
)upstart
had a very brief existence and was replaced withsystemd
Making a systemd service
Supposing you decide to name your service
myservice
, create a file/etc/systemd/system/myservice.service
:3 useful commands:
sudo systemctl enable myservice
makes the service alwasy begin on system startupsudo systemctl start myservice
manually starts the servicesudo systemctl stop myservice
manually stops the serviceFor additional config options and legal values that you can put into your service file, see https://www.freedesktop.org/software/systemd/man/systemd.service.html#Options
pleaserun
is a ruby script that attempts to solve the issue of automatically creating an init script with a single command. quoting from it's page:"With pleaserun, you can generate the following launchers/scripts/whatever:
launchd
upstart
systemd
runit
sysv init"
It also detects which init system is in use so it generates the script accordingly.