325

I have a daemon that runs fine if I start it manually with the service command:

ricardo@ricardo-laptop:~$ sudo service minidlna start                   
 * Starting minidlna minidlna                                                              [ OK ] 

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?

boardrider
  • 227
  • 5
  • 18
Ricardo Reyes
  • 4,097
  • 4
  • 21
  • 17
  • @user154721 What arguments did you give update-rc.d to make autostart work? I've tried various options but haven't had any luck. – Vilāsamuni Aug 13 '13 at 10:41
  • Auto-start programs are configured in `autostart` manifests or in `*.service` files in several locations, as well as in `init.d` or `crontab`. See: https://unix.stackexchange.com/a/525845/43233 – Noam Manos Jun 20 '19 at 06:34

5 Answers5

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

Do the command

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

LassePoulsen
  • 14,517
  • 8
  • 47
  • 59
  • 7
    thanks, but what if I get this output when running that command?: `System start/stop links for /etc/init.d/minidlna already exist.` – Ricardo Reyes Oct 25 '10 at 16:02
  • I grep'd for minidlna in all the files at /var/log and couldn't find any mention of the service at startup – Ricardo Reyes Oct 25 '10 at 16:03
  • 1
    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. – LassePoulsen Oct 25 '10 at 16:20
  • 8
    @RicardoReyes use `sudo update-rc.d -f minidlna remove` to remove the existing links. – Dogweather Jun 09 '14 at 06:04
  • 5
    I am getting below error update-rc.d: /etc/init.d/usermanage: file does not exist – Rizwan Patel Mar 14 '16 at 08:17
  • Also getting the error that @RizwanPatel is getting – Brian Leishman Sep 06 '18 at 20:13
  • 6
    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. – explorer Jan 25 '19 at 03:42
53

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
tanius
  • 6,053
  • 1
  • 37
  • 49
  • 1
    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. – Cutton Eye Mar 18 '21 at 09:35
  • @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 Apr 13 '22 at 09:29
  • @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. – tanius Apr 14 '22 at 16:54
50
  • To start a daemon at startup:

    update-rc.d service_name defaults
    
  • To remove:

    update-rc.d -f service_name remove
    

defaults => default run levels 2,3,4 and 5

Example:

update-rc.d tomcat7 defaults
David Foerster
  • 35,754
  • 55
  • 92
  • 145
Lakal Malimage
  • 601
  • 5
  • 2
  • 2
    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". – John Little Feb 08 '16 at 15:51
  • 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 – axk Jul 07 '18 at 21:40
40

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.

  1. Open /etc/rc.local file with this command:

    vim /etc/rc.local
    
  2. Add your script that you want to run on boot process there, for example:

    sh /home/ivan/iptables.sh 
    echo 'Iptable Configured!'
    
  3. Review the comments included in that file and make sure an exit 0 is at the end.

  4. Save the files. And your script will run on boot process.

techraf
  • 3,306
  • 10
  • 26
  • 37
hhlp
  • 41,392
  • 18
  • 84
  • 133
  • 2
    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? – Sanghyun Lee Apr 11 '13 at 05:29
  • Would this method issue "shutdown" command to the service or just kill the process upon OS shutdown? – Vadym Chekan Jul 25 '14 at 00:25
  • 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. – Weboide Dec 04 '14 at 12:45
  • 1
    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 – BobTuckerman Jan 05 '17 at 21:18
3

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
iqmaker
  • 131
  • 3