Setting up Linux init.d service
-
First, write a server configuration file, which would be placed inside
/etc/init.d
directory. Note, that your service name should (must) end with letter 'd'. Example:xmrigd
. Configuration file (/etc/init.d/myserviced
) should look as follows (newlines matter):#!/bin/sh ### BEGIN INIT INFO # Provides: myservice # X-Start-Before: # X-Start-After: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: Runs myservice service # Short-Description: Runs myservice service ### END INIT INFO export PATH=$PATH:/usr/bin:/bin case "$1" in start) (/usr/bin/myprogram &) # Manually fork here, it is important ;; stop) ps aux | grep -i myprogram | awk '{ print $2 }' | xargs kill -TERM ;; *) echo "Usage: /etc/init.d/myserviced {start|stop}" exit 1 ;; esac exit 0
Provides
. Name of the server without letter 'd' (again, it is not necessary, but just for good look at least ...)Description
. Description of the server. You can type anything you want here, but you should avoid newlines.Short-Description
. Short description of the server. You can type anything you want here, but you should avoid newlines.
In this example, service script just executes
/usr/bin/myprogram
and callsfork
. Sometimes you may find out that the service does not see this executable (even if it is located in/bin
directory). This is normal, just set thePATH
variable in your service script (take a look at stackoverflow solutions). -
Since this script is not a
systemd
service, we should convert it to one. This can be done withupdate-rc.d
command:update-rc.d myserviced defaults
-
Now, you can start your service by running:
systemctl start xmrigd.service
-
Make your service run on startup:
systemctl enable xmrigd.service
-
Verify that your service is running withour unwanted errors (press 'q' button to exit out of interactive menu):
systemctl status xmrigd.service