How do I prevent Linux services from auto-starting?

10

2

I have recently migrated from Windows to Linux (xubuntu)

I am a developer and have installed everything I need, LAMP. In Windows I used to turn off all unnecessary services - I don't need the Apache or MySQL service running all the time. Whenever I needed MySQL I used to use:

net start mysql

How do I do the same in Linux?

  1. Disabling not-needed daemons from auto-starting?
  2. Starting them only when I need them?

Aviv

Posted 2010-10-27T08:50:49.323

Reputation: 558

3I think you should include the exact version of ubuntu that you are using. New ubuntu versions use upstart, which has its own gotchas. – vtest – 2010-10-27T09:55:55.867

Edit:I am usning Xubuntu latest version 10.04, but id don't want to use and GUI for this, only from terminal. Looks like @prhq got something in his answer. What is upstart? – Aviv – 2010-10-27T13:30:14.423

Belongs elsewhere, either on Server Fault or Unix. Still useful though. – ripper234 – 2011-06-11T16:44:41.800

Answers

10

In most linux distributions you can manually start/stop services by (as root or using sudo) running the following commands:

# /etc/init.d/apache2 start
# /etc/init.d/mysqld start

# /etc/init.d/apache2 stop
# /etc/init.d/mysqld stop

Which services that are automatically started is controlled by file links in /etc/rc[runlevel].d/ . Find your current runlevel by using the command "runlevel" as root

# runlevel
N 2

Which here indicates runlevel 2 Now you just have to remove those files in /etc/rc2.d/ which you don't want started.

Removing apache and Mysql on a desktop is usually ok, but be aware of removing other services.

hultqvist

Posted 2010-10-27T08:50:49.323

Reputation: 287

3This is rather misleading, even if you said "most distributions". I would qualify your recipe as distro specific. – vtest – 2010-10-27T09:57:04.977

Which did you have in mind? I can only think of ArchLinux(but those users should already know what they are doing). Of course some distros have specific tools, but the above technique works on them as well. – hultqvist – 2010-10-27T10:47:51.900

Quite strange, i can see a file called S91apache2 in the /etc/rc2.d directory, i guess it starts the apache2 ... but i can't find any file regarding MySQL. Where can i learn about this auto-starting daemons? – Aviv – 2010-10-27T13:34:01.787

Then the mysql server might not be configured for automatic start. This site looks like having a good explanation: http://www.yolinux.com/TUTORIALS/LinuxTutorialInitProcess.html

– hultqvist – 2010-10-27T17:44:09.553

Note that under *BSD and the Slackware tree, the directory is /etc/rc.d/. – new123456 – 2012-03-28T11:03:46.367

8

For Ubuntu versions that use systemd (15.04 and later) use:

systemctl disable service

This will do the job. It will disable the service and won't restart after a reboot. To temporarily enable simply start the service. Not enable.

To find the service name use

service --status-all

Other commands are:

systemctl start service - Use it to start a service. Does not persist after reboot

systemctl stop service - Use it to stop a service. Does not persist after reboot

systemctl restart service - Use it to restart a service

systemctl status service - Shows the status of a service. Tells whether a service is currently running.

systemctl enable service - Turns the service on, on the next reboot or on the next start event. It persists after reboot.

systemctl disable service - Turns the service off on the next reboot or on the next stop event. It persists after reboot.

Nandesh

Posted 2010-10-27T08:50:49.323

Reputation: 181

2It's a pity it's not the accepted answer :). Thank's, I totally forgot about that command. – Nordes – 2019-04-22T00:59:54.680

8

Ubuntu 10.04 is in the middle of a transition between two service management systems: SysVinit (the traditional system, used by most Linux distributions) and Upstart (a newer system pushed by Ubuntu and becoming available in more and more distributions).

SysVinit service management scripts are in /etc/init.d. You can start the service with /etc/init.d/SERVICENAME start and stop it with /etc/init.d/SERVICENAME stop. Whether the service is started automatically on boot depends on the presence of symbolic links in /etc/rc?.d where ? is a digit from 2 to 5 (the runlevel). The easiest way to prevent a service from starting automatically on boot is to use update-rc.d SERVICENAME disable.

Upstart service management configuration files are in /etc/init. You can start the service with start SERVICENAME and stop it with stop SERVICENAME. The configuration file /etc/init/SERVICENAME.conf contains a line indicating when to start the service: start on …. An easy way of disabling these services is to change that line to start on never and (…). If you don't want to edit the file, you can also completely disable the service without confusing the packaging system by renaming it to not end in .conf.

dpkg-divert --add --local --divert /etc/init/foo.conf.disabled --rename /etc/init/foo.conf

As of Ubuntu 10.04, Apache comes with a SysVinit script and Mysql comes with an Upstart script.

Gilles 'SO- stop being evil'

Posted 2010-10-27T08:50:49.323

Reputation: 58 319

Is editing the servicename.conf really the preferred way? Especially when updates can theoretically update those configuration files and override your changes – Masse – 2011-04-21T14:23:00.123

@Masse: It's not always the preferred way, but it has the advantage of always working. Some services read a file in /etc/default and have a START_FOO option there that you can turn off, but many expect to run if they're installed. Updates won't overwrite your changes without prompting since these are all conffiles. – Gilles 'SO- stop being evil' – 2011-04-21T19:07:57.043

Wow. This seems like a major step backwards. – Masse – 2011-04-26T20:43:06.480

@Masse: Conffiles are meant to be edited by the administrator. But if you don't want to do that, you can also use dpkg-divert to rename the service file. However if you do that you won't be able to start the service explicitly. – Gilles 'SO- stop being evil' – 2011-04-26T21:17:00.077