4

I'm looking for a command to stop all services running and probably restart them after. Any help would be appreciated.

muru
  • 197,895
  • 55
  • 485
  • 740
Brian K
  • 41

2 Answers2

4

assuming you are using ubuntu 16.04 you can use systemctl to control services.

to get a list of services and their status

systemctl | grep service

to control an individual service , replace service with the service name

systemctl status service
systemctl stop service
systemctl start service

in older version of ubuntu services can be controlled via init scripts which live in /etc/init.d , these work in a similar way

to get a list of services

ls -l /etc/init.d/

to contrl and individual service

/etc/init.d/service status
/etc/init.d/service stop
/etc/init.d/service start
Amias
  • 5,246
2

Although I highly recommend rebooting the machine instead, you can write a command to achieve this:

Note: This may force a machine reboot halfway through at which point this effort is profitless.

for s in `systemctl | grep service | cut -d '.' -f 1`; do sudo systemctl restart $s; done

I would instead list all currently running services, identify the ones I want to stop, then manually restart each service:

Note: restart internally calls start + stop so you won't have to perform them separately.

  1. List currently running services:

    systemctl | grep running

  2. Restart each service by typing:

    systemctl restart <service>