How to Start Linux Daemon program at boot up
I am confused on how to start my Daemon C program at boot-up. The program runs as a Daemon OK when I satrt it from command shell, but now I want it to start up every time at boot-up. I have searched for the last week on how to do this and there many confusions on how this is done — easily and simply? I am running Unbuntu 11.10 and don’t really want to put in the the Ubuntu Startup files — it works but only after the user has logged-on. I want it to start-up even if the user has not logged in — just like apache2 server that I have which starts up after boot-up — plain and simple. What I have found is that I need to create a init script and put in in the /etc/init.d/ directory but am not sure how to do this properley? My Daemon is executable and located at /usr/local/bin/myDaemon and to run it from the command shell I simply use /usr/local/bin/myDaemon to run it? Can someone please show me a simple basic exapmple script that I can use to get me started?
2 Answers 2
Don’t forget to call the daemon library function in your program.
Then, create a /etc/init.d/yourdaemon script taking /etc/init.d/skeleton as a model (init script vary from distribution to distribution).
You could also create a crontab entry for your daemon, using @reboot as the time specification.
You also need to symlink your /etc/init.d/yourdaemon script into an appropriate place in the SysV RC hierarchy (the init manpage on your system will usually tell you which runlevels are for what, and finding the right spot within a runlevel is usually pretty self-explanatory. Note that Ubuntu has started using Upstart (upstart.ubuntu.com) in place of the traditional SysV init scripts and best practices are different there.
How to start a daemon at boot time after all other services are ready
I have developed a daemon in Linux. It works correctly, however, when I set that script to be started at boot time, it has conflict with other service, so I need to start it after all services have started. How can I add a delay in the init.d shell script or inside the service itself without affecting booting the system? This is Debian Linux. Regards Jaime
First thing to do is understand why it has conflict with another service. Once this step is achieved, you can think about a solution. And if you want to be helped, please share evidences (command, logs, error msg, . ) of your assumptions. You can even share your debian version, just to know if you are supposed to use systemd instead of legacy /etc/init.d subsystem.
I know why it has the conflict. This is not a computer but an RFID device. My daemon uses a hardware module that is initialized by other daemon. If both daemons are initialized at the same time, that hardware module get blocked. In short, my daemon should be started after that hardware module is completely initialized, that is, after the daemon that initializes that module has done it. This is the OS version: Debian GNU/Linux 7.8 (2015-01-27)
1 Answer 1
one possible option is to include such a piece of code in your init.d script.
of course you need to change the test part lsmod | grep -qw module_name to verify that the hardware module is fully initialized.
also adjust the startup of your daemon, depending if it is daemonizing itself or not.
be sure that first init script /etc/rcX.d/S98first_daemon is launched before your homemade daemon startup script /etc/rcX.d/S99homemade_daemon
#!/bin/bash wait_period=0 sleep_period=5 max_wait_period=30 function is_other_daemon_fully_initialized() < lsmod | grep -qw module_name && return 0 return 1 >while true do echo "Time Now: `date +%H:%M:%S`" echo "Sleeping for $sleep_period seconds" wait_period=$(($wait_period+$sleep_period)) if [ $wait_period -gt $max_wait_period ];then echo "Max wait period exceeded, abort." exit 1 else sleep $sleep_period is_other_daemon_fully_initialized && break fi done echo "Initialisation done in $wait_period seconds, launching second daemon." /path/to/your/homemade/daemon
Linux daemon start up
i wrote one service on linux(Redhat Server Edition 5.1) . which is started by shell scritpt, In case when i start my application i manually start my service , now i want to start my service at boot time,by means i put my service on init.d folder by my daemon not invoke at boot time,any have idea how to start a daemon at boot time on linux? this my sample but is not working
#!/bin/sh # # myservice This shell script takes care of starting and stopping # the # # Source function library . /etc/rc.d/init.d/functions # Do preliminary checks here, if any #### START of preliminary checks ######### ##### END of preliminary checks ####### # Handle manual control parameters like start, stop, status, restart, etc. case "$1" in start) # Start daemons. echo -n $"Starting daemon: " echo daemon echo ;; stop) # Stop daemons. echo -n $"Shutting down : " killproc echo # Do clean-up works here like removing pid files from /var/run, etc. ;; status) status ;; restart) $0 stop $0 start ;; *) echo $"Usage: $0 " exit 1 esac exit 0
4 Answers 4
Put 2 comments into your script:
# chkconfig: - 90 10 # description: description of your service
These lines will tell chkconfig in which runlevel your script is supposed to run, and the start and stop priorities. See this : linux.die.net/man/8/chkconfig
if the service is already listed, you may need to run chkconfig —del my_service before you do the —add
Set daemon to start at boot with systemd
I’m writing a daemon to manage my Java app on a headless Ubuntu 16.04 box using jsvc and this (probably pre-systemd) tutorial, and got as far as running update-rc.d mydaemon enable , receiving the error update-rc.d: error: mydaemon Default-Start contains no runlevels, aborting Having Googled around a bit this appears to have something to do with the (fairly?) recent move to systemd , which I have confirmed is running with pidof systemd . How do I achieve the same starting-at-boot behaviour as update-rc.d (and more importantly stopping the service via /etc/init.d/mydaemon stop rather than just killing the process as the Java app needs to clean up). And are systemd and update-rc.d different systems, or does systemd just change how the latter works?
2 Answers 2
I don’t have a Ubuntu 16.04 to test this on, or provide you with many details, but systemd has a compatibility feature to allow older /etc/init.d scripts to continue working. Instead of using update-rc.d to enable your daemon, use the systemd native command equivalent:
sudo systemctl enable mydaemon
If this still produces the same error, add the missing lines to the starting set of comments in your script:
# Default-Start: 2 3 4 5 # Default-Stop: 0 1 6
between the ### BEGIN INIT INFO and ### END INIT INFO lines, and try again. See the LSB core description for these lines. You can also explicitly start the daemon with
sudo systemctl start mydaemon
and ask for its status with
sudo systemctl status -l mydaemon
See man systemd-sysv-generator for the compatibility feature. See this wiki for converting System V or upstart scripts like yours to native systemd Units.
Ah thank you. I’d inadvertantly done this as I’d made a new systemd script in /usr/systemd but still came up with these errors. I figured since the systemd script referenced the systemv one, that’s why it was causing the errors and so I added these lines in the systemv script. Didn’t realise systemd was automatically converting systemv scripts into systemd ones.
You must have a /etc/init.d/tightvncserver script like this:
#!/bin/sh ### BEGIN INIT INFO # Provides: tightvncserver # Should-Start: # Required-Start: $local_fs $remote_fs x11-common # Required-Stop: $local_fs $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: VNC server # Description: Debian init script for the VNC Server ### END INIT INFO # /etc/init.d/tightvncserver # Set the VNCUSER variable to the name of the user to start tightvncserver under VNCUSER='pi' case "$1" in start) su $VNCUSER -c '/usr/bin/tightvncserver :1 -geometry 1280x800 -depth 16' echo "Starting TightVNC server for $VNCUSER" ;; stop) pkill Xtightvnc echo "Tightvncserver stopped" ;; *) echo "Usage: /etc/init.d/tightvncserver " exit 1 ;; esac exit 0
sudo service tightvncserver start
How can I configure a service to run at startup
but it’s not configured to auto start when the PC reboots. How can I configure it to start automatically, even if no one is logged into the PC?
@user154721 What arguments did you give update-rc.d to make autostart work? I’ve tried various options but haven’t had any luck.
Auto-start programs are configured in autostart manifests or in *.service files in several locations, as well as in init.d or crontab . See: unix.stackexchange.com/a/525845/43233
5 Answers 5
sudo update-rc.d minidlna defaults
This should add the service to the automatic startup system. But if you get:
System start/stop links for /etc/init.d/minidlna already exist.
sudo update-rc.d minidlna enable
P.S.: For further detail look at the man page for update-rc.d by typing the command man update-rc.d
thanks, but what if I get this output when running that command?: System start/stop links for /etc/init.d/minidlna already exist.
if minidlna doesnt write to any logfile you won’t find it any where. Does it maybe fail because there is no network connection when it is started? Try to look at the configuration if it is possible turn on debugging and/or logging to find out whats wrong.
I am also, got the same error update-rc.d: /etc/init.d/mongod: file does not exist . Then, sudo systemctl enable mongod.service worked for me.
Since Ubuntu 15.10 and newer (resp. Debian 8 «jessie» and newer), you have to use the following command to configure your service minidlna to run at startup:
sudo systemctl enable minidlna.service
And to disable it again from starting at boot time:
sudo systemctl disable minidlna.service
This works with all service names available on your system. To find out available service names, just list the filenames of the service files:
ls /lib/systemd/system/*.service ls /etc/systemd/system/*.service
As for sudo, I recieve Unknown operation enable. If I discard sudo it will ask me which user I would like to choose. Pick your root user and you will be able to set the desired value.
@tanius I have followe the procedure of adding new services to /etc/systemd/system/*.service. I would like to know why no one suggested creating or editing these files as it seems to provide mode options (Restart, RestartSec, ExecStop, etc. )
@tiagoams Creating a service file was just not the question here, since the OP states that a service minidlna already exists but does not autostart. But thanks for the pointer to service files under /etc , I added that to the answer now.
update-rc.d service_name defaults
update-rc.d -f service_name remove
defaults => default run levels 2,3,4 and 5
update-rc.d tomcat7 defaults
When I do this command, I get «System start/stop links for /etc/init.d/tomcat7 already exist». however, when I reboot, it does not start tomcat, I always have to do «service tomcat7 start».
in my case sudo update-rc.d myservice default always completes with no output and the service won’t start on boot-up or with sudo service myservice start which also completes silently. sudo /etc/init.d/myservice start works however
Sometimes you need to run a script on boot process, for example run an iptables config at boot process. So you don’t have to run the script manually every rebooting.
You can run your script on boot process in Ubuntu by adding it to /etc/init.d/rc.local file. Look the steps below.
- Open /etc/rc.local file with this command:
sh /home/ivan/iptables.sh echo 'Iptable Configured!'
Comments of /etc/init.d/rc.local in Ubuntu 12.04 says «Short-Description: Run /etc/rc.local if it exist», so maybe adding the scripts to /etc/rc.local would be better idea?
Vadim, rc.local is just run at boot, nothing is achieved on shutdown. The process would most likely be killed by the OS on shutdown.
FYI: The difference between rc.local vs adding it to init, is that rc.local is executed at the end of the init startup sequence, rather than as part of it
In ubuntu version 18.04 TLS, I found that update-rc.d does not work fine if there is no specific comment block in the start script that looks like this:
### BEGIN INIT INFO # Provides: myprogram # Required-Start: $ local_fs $ remote_fs $ syslog $ network $ time # Required-Stop: $ local_fs $ remote_fs $ syslog $ network # Default-start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: myprogram some description ### END INIT INFO