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
How to Configure a Linux Service to Auto-Start After a Reboot or System Crash: Part 1 (Practical Examples)
In computing, things don’t always go as planned. Oftentimes unexpected system crashes prompt system admins to initiate reboots and restart of individual services. Figuring out and restarting every service your application needs to run after a system crash or reboot can be tedious. In this first installment of the two-part tutorial, we will show you how to configure services to automatically start after a system crash or server reboot with practical examples. The second part will cover theoretical information about what we achieved in part one.
We will use the MySQL database service for practical examples. However, the same principles apply to other processes that make up a complete server such as Nginx, Apache, Redis, or other applications. You can check out our tutorials on how to install MySQL, Nginx, and Apache.
In Linux distributions, there are three major initialization (init) systems, depending on the distribution you are running. Some distributions may come with two or more init systems as outlined below:
- System V – an older init system found in older distributions like:
- Ubuntu 9.04 and earlier
- CentOS 5 and earlier
- Debian 6 and earlier
- CentOS 6
- Ubuntu 9.10 to Ubuntu 14.10 and Ubuntu 14.04
- CentOS 7
- Debian 7 and 8.
- Ubuntu 15.04 and newer
Background
It’s common for operating systems, and especially Linux and Unix systems to have processes and services running in the background. Such services may have shipped with the operating system software. Some may have come with the user applications that you install.
Operating System services include:
- sshd – It is the daemon allowing remote connections.
- cupsd – It is the daemon controlling printing.
Installed application services include:
- httpd/apache2 – A service that comes with Apache2 web server.
- nginx – A service that comes with Nginx web server.
To ensure that our web applications, databases, mail servers, etc. are accessible, such services must run continuously. If you are a system admin or a curious app developer, you want to ensure that such services run continuously, and in the unfortunate event of a system crash, they start automatically after the system reboots. And that is exactly what we will be learning in this hands-on tutorial.
While setting alerts and continuously monitoring your Linux distribution is crucial, some Linux services can self-heal if well configured, thanks to the init systems that manage services.
In Linux distributions, there are mode operations that implement system initialization called runlevels. For a service to auto-start, it must be added to a runlevel. Every Linux and Unix like system has four common runlevels as listed below:
- 0 – Runlevel 0 indicates system shutdown.
- 1 – Runlevel 1 indicates single-user, rescue mode.
- 2, 3, 4 – These runlevels indicate states where the system has booted in multi-user, network-enabled, text mode.
- 5 – Runlevel 5 indicates multi-user network-enabled, graphical mode.
- 6 – Runlevel 6 indicates system reboot.
In this tutorial, you will learn how to configure a Linux service to automatically start when the system reboots using the three different init modes explained earlier: System V, Upstart, and Systemd.
Prerequisites
This hands-on tutorial expects you to have a Linux VPS that you can use to follow along. You may take advantage of the free trial period at Cloudsigma and fire up some servers to try out the commands. You can follow along with our step-by-step tutorial on how to set up your Ubuntu servers.
The servers you create in this tutorial are purely for following up with the hands-on practicals and you should not be trying out the commands on a production server since a lot of services will be disrupted.
Some of the distributions that you will need:
- Ubuntu 9.04 and earlier, or Debian 6 x64 (will be used to demonstrate System V init system)
- Ubuntu 14.04 x64 (will be used to demonstrate Upstart)
- CentOS 7 x64 (will be used to demonstrate systemd).
Ensure you have set up a non-root user with sudo privileges. You can take a look at our tutorial on configuring the sudoers file here.
Using System V
This is the oldest init system which was used in earlier Linux distributions such as:
Most installable server applications such as MySQL and Nginx come with init scripts saved in the /etc/init.d directory by default. These scripts enable them to start after a reboot. However, they may not be configured to start automatically after a system crash.
Auto-start Checklist for System V
The first step is to check for the availability of a functional Bash init script at /etc/init.d/service directory. To enable the service, in Debian or Ubuntu distributions, use the update-rc.d command, in a CentOS system, use chkconfig. Replace with your actual service name: