Linux daemon on startup

How to correctly add a custom daemon to init.d?

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?

6 Answers 6

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:

# tty1 - getty # # This service maintains a getty on tty1 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[2345] stop on runlevel [!2345] respawn exec /sbin/getty -8 38400 tty1 

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!

  1. save your program that will run as a service in /usr/sbin sudo cp myprogramname /usr/sbin/myscriptname
  2. create a basic startup script (use /etc/init.d/skeleton as reference)
  3. move this script to /etc/init.d sudo mv /etc/init.d/myscriptname
  4. give this script executable permission (i used 775, but you can set it lower) sudo chmod 755 /etc/init.d/myscriptname
  5. goto /etc/init.d cd /etc/init.d
  6. include in startup list with low startup priority sudo update-rc.d myscriptname defaults 97 03
Читайте также:  Linux console public ip

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:

cd /etc/init.d sudo service myscriptname start 

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).

#! /bin/sh ### BEGIN INIT INFO # Provides: # Required-Start: $remote_fs # Required-Stop: $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Sample_GT02 daemon startup script # Description: Sample Server for GT02 class ### END INIT INFO # Author: Tony Gil # # Do NOT "set -e" # PATH should only include /usr/* if it runs after the mountnfs.sh script PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="Sample Daemon" NAME=sampleserver_gt02 DAEMON=/usr/sbin/$NAME PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME CHUID=root # Exit if the package is not installed [ -x "$DAEMON" ] || exit 0 # Read configuration variable file if it is present [ -r /etc/default/$NAME ] && . /etc/default/$NAME # Load the VERBOSE setting and other rcS variables . /lib/init/vars.sh # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions # # Function that starts the daemon/service # do_start() < # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test >/dev/null \ || return 1 start-stop-daemon --start --quiet --chuid $CHUID --pidfile $PIDFILE --exec $DAEMON -- \ $DAEMON_ARGS \ || return 2 > # # Function that stops the daemon/service # do_stop() < # Return # 0 if daemon has been stopped # 1 if daemon was already stopped # 2 if daemon could not be stopped # other if a failure occurred start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME RETVAL="$?" [ "$RETVAL" = 2 ] && return 2 # Wait for children to finish too if this is a daemon that forks # and if the daemon is only ever run from this initscript. # If the above conditions are not satisfied then add some other code # that waits for the process to drop all resources that could be # needed by services started subsequently. A last resort is to # sleep for some time. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON [ "$?" = 2 ] && return 2 # Many daemons don't delete their pidfiles when they exit. rm -f $PIDFILE return "$RETVAL" ># # Function that sends a SIGHUP to the daemon/service # do_reload() < # # If the daemon can reload its configuration without # restarting (for example, when it is sent a SIGHUP), # then implement that here. # start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME return 0 >case "$1" in start) [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" do_start case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; stop) [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" do_stop case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; #reload|force-reload) # # If do_reload() is not implemented then leave this commented out # and leave 'force-reload' as an alias for 'restart'. # #log_daemon_msg "Reloading $DESC" "$NAME" #do_reload #log_end_msg $? #;; restart|force-reload) # # If the "reload" option is implemented then remove the # 'force-reload' alias # log_daemon_msg "Restarting $DESC" "$NAME" do_stop case "$?" in 0|1) do_start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running *) log_end_msg 1 ;; # Failed to start esac ;; *) # Failed to stop log_end_msg 1 ;; esac ;; *) #echo "Usage: $SCRIPTNAME " >&2 echo "Usage: $SCRIPTNAME " >&2 exit 3 ;; esac : 

Источник

Run Qt application on startup as Linux daemon

I’ve developed a Qt application which contains a TCP server and such. I’m now trying to make Ubuntu packages and let the application automatically start on startup. The application needs to be running even if nobody is logged in, which means a daemon started via a script in /etc/init.d/ I tried simply running the application on start and sending a kill-signal on stop in the init.d script but that means the application runs in the foreground and blocks the init-script. Forking like in an other question almost seems to work, I get ‘unknown error’ after trying to start a TCP server. Nevertheless, there should be an easy to way to write a init-script that runs my application in the background on startup on the various Linux distributions. Could anyone point me in the right direction? Using Ubuntu 9.10 with Qt 4.5

5 Answers 5

The best way is probably to use QtService where the work of forking is taken care of for you.

However, if you want to continue to build your own, you should either background the application or run it via start-stop-daemon that comes with OpenRC or a similar utility for your distribution.

Also, make sure that you only link to the QtCore shared library. Although the application might be command line and never pull up the GUI, that doesn’t mean that X isn’t required in order for the application to run. For example, a set of unit tests:

$ ldd runTests | grep Qt libQtTest.so.4 => /usr/lib/qt4/libQtTest.so.4 (0x00007fd424de9000) libQtXml.so.4 => /usr/lib/qt4/libQtXml.so.4 (0x00007fd424baa000) libQtGui.so.4 => /usr/lib/qt4/libQtGui.so.4 (0x00007fd4240db000) libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0x00007fd422644000) 

Because QtGui is present, all the X libraries are also brought in, although filtered from the above output.

Источник

How to autostart a daemon after installation?

I’m writing a daemon and packaging it up but I want the daemon to run after install. Is there a way to make the daemon run after it gets installed?

2 Answers 2

Thanks for the links, that’s exactly what I was thinking about (post install scripts) a few days after I posted this question.

You daemon init script is located in /etc/init.d/

To enable it for execution after start

update-rc.d daemon defaults 

Other options you’ll find in man update-rc.d .

And use systemctl enable to autostart a service.

I’m a little new to daemon programming on Linux. I only see a config file in /etc/init/ , nothing in /etc/init.d/ .

Ok, so if I package the init script to copy to /etc/init.d/ at install time, will this start the daemon once install has completed?

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector