Services management Essentials for Devops
What you will Learn:
Remove package using apt-get remove
Remove package using apt-get purge
Clear cache
Install nginx webserver (nginx as background service)
systemctl command to view all services
systemctl command to view only running services
systemctl command to stop a service
systemctl command to start a service
systemctl command to view status of a service
Run nginx as foreground service (daemon off)
active (exited) status
Before we talk about managing services, let us see few more topics on packages.
Remove package using apt-get remove
‘Remove’ method will remove the binaries but it keeps the config files. To remove a package, the syntax is:
‘sl apt-get remove <package name>’
Remove package using apt-get purge
‘purge’ removes everything. We can purge and free some disk space by executing
‘sudo apt autoremove’ or ‘sudo apt-get autoremove’
Clear cache
The command ‘sudo du -sh /var/cache/apt’ tells us the amount of space currently used by cache
We can clear cache by executing ‘sudo apt-get clean’
Install nginx webserver (nginx as background service)
‘nginx’ is a popular webserver, let us install it by executing
‘sudo apt-get install nginx’
After installation, note down the ip address of ubuntu
Open a browser (either on your local machine host or ubuntu machine), type the above ip address and hit ‘Enter’.
You should see nginx webserver running, see below. So nginx is running as a background service and is serving the default webpage
systemctl command to view all services
We use systemctl command to manage the services. We can execute the command ‘sudo systemctl --type=service’ to look at all the services (whether running or not). Notice below nginx is running as a service
systemctl command to view only running services
We can execute the below command to look at only those services that are running:
sudo systemctl --type=service --state=running
Notice above that nginx.service is running
systemctl command to stop a service
We can use ‘sudo systemctl stop <service>’ to stop a service
Refresh the webpage, notice below that nginx service is down
systemctl command to start a service
We can use ‘sudo systemctl start <service>’ to stop a service
Refresh the webpage, notice below that nginx service is now up
systemctl command to view status of a service
We can use the command to see the status of a particular service sudo systemctl status <service name>
Below means that nginx service is currently ‘active (running)’. Also nginx service is running as a daemon (since daemon is ‘on’ as you can see below) viz running in the background
However you would see ‘inactive (dead) when you stop the service
Run nginx as foreground service (daemon off)
So far we have been saying that nginx service is running in the background viz daemon is on (see the line below:
/usr/sbin/nginx –g daemon on;)
Please note that /usr/sbin/nginx means that nginx binary is present inside the path /usr/sbin/
Now, we can also run the service in the foreground with daemon off. Before we do that, let us first stop the service
Execute the below command to start service in foreground:
sudo /usr/sbin/nginx –g ‘daemon off;’
Refesh browser
Nginx service is now running in the foreground.
Ctrl+C to kill the nginx process
Refresh the page, notice below that foreground service is stopped (since nginx webserver is not running)
Nginx generally runs in the background (daemon on), let us again restart the service in background
So, in summary, ‘daemon on’ means service is running in background and ‘daemon off’ means service is running in foreground.
active (exited) status
Sometimes you might see active (exited) status
To resolve this, you can try to execute below command
After that stop the service
Next start the service
Now the status will show active (running)
Thank you for reading!