Hi friend,
Starting services manually every time your Linux server reboots can really be a pain! But don‘t worry, there are simple ways to configure auto-start on boot that‘ll save you time as a system admin. In this guide, I‘ll dig into the details so you can master auto-starting services on major Linux distros.
Why Auto-start is Your Friend
Enabling auto-start for services gives you some nice benefits:
-
Minimizes downtime – Services spring back to life immediately after rebooting instead of waiting for you to get around to starting them manually. Critical for uptime!
-
Saves you effort – No more wasted time having to SSH in and start everything one by one after a reboot. You‘re freed up for bigger tasks.
-
Robustness – Auto-start means services will consistently start even after unexpected crashes or power outages. Can‘t count on yourself to be there!
-
Consistency – Services start up in a defined order every single time, avoiding thorny issues with dependencies. Way better than doing it manually.
With auto-start correctly configured, you can kick back knowing your system will take care of itself on reboot. Pretty slick eh?
Now let‘s look at how auto-start works on some common Linux distros…
Auto-Starting Like a Pro on CentOS/RHEL 6
CentOS and RHEL 6 use an init system called System V init. That means auto-start is handled by good ol‘ init scripts together with a tool called chkconfig
.
Here‘s how to set a service like Nginx to auto-start:
-
Become the almighty root user on your server.
sudo su
will do the trick. -
Create an init script for Nginx in
/etc/init.d/
– let‘s call itnginx
-
Make your script executable:
chmod +x /etc/init.d/nginx
-
Use chkconfig to activate auto-start:
chkconfig --add nginx
chkconfig nginx on
- Verify Nginx will now start on boot:
chkconfig --list nginx
The output should show Nginx as on
for levels 2, 3, 4, and 5. High five!
I know what you‘re thinking…this chkconfig stuff is a bit archaic compared to newer systems. But it gets the job done!
To undo auto-start, just run chkconfig nginx off
and chkconfig --del nginx
.
systemd – New Hotness for CentOS/RHEL 7 and 8
Newer CentOS and RHEL versions use systemd for their init system and service management. That gives you access to some nice new commands while still letting you auto-start services.
Here‘s what you need to do:
-
Make sure the service unit file exists in
/usr/lib/systemd/system/
(it‘s there already for common ones like Nginx) -
Run this magic command:
systemctl enable nginx.service
Under the hood this just sets up a symlink to auto-start the service. Pretty slick!
- Verify it worked:
systemctl status nginx.service
The service should show as enabled and active. High fives again!
To undo, just tell systemctl to disable
instead of enable. Nice and clean.
While the systemd way may seem foreign at first, it makes service management much more robust once you get used to it.
Ubuntu – Using Upstart
Ubuntu uses Upstart as its init system together with classic System V init scripts. Here‘s how you get auto-start set up:
-
Create an init script for your service in
/etc/init.d/
as needed -
Run this command:
update-rc.d nginx defaults
This sets up the default auto-start links. The service will now launch on boot!
To undo, just run update-rc.d -f nginx remove
. Nice and easy.
Upstart gives you some of the robustness of systemd without going all in. A nice middle ground!
Parting Advice
Auto-starting services the right way keeps your system humming along with minimal downtime. Follow the examples I showed for your Linux distro and you‘ll be golden!
Here are a few other tips that can save you headaches:
-
Poke around in
/var/log/boot.log
if a service won‘t start right on reboot. The clues are in the logs! -
Use
systemctl status
orservice nginx status
after booting to verify the service is really running. -
Carefully order your auto-start services if they depend on each other.
-
Test reboots often – before they happen for real!
So get out there and configure auto-start for your services. It‘s like giving your future self a high five! Let me know if you have any other questions.