Managing Services On Linux
Managing Services On Linux
In this lab, we'll use a number of Linux commands that were already explained during Course 3. Here is a reminder of what these commands do:
We will also present a number of new commands such as service, logger. We will briefly explain what these commands do when they are
shown. Remember that you can always read the manual page using man <command_name> to learn more about a command.
While you can copy and paste the commands that are presented in this lab, we recommend typing them out manually, to help with understanding
and remembering them.
Let's look at the services that are installed in the machine. In order to do this, we will use the service command.
If you run service with parameters --status-all, it lists the state of services controlled by System V.
If you are interested in seeing only the services that are running, you can use the following command:
Here you will see the following notations with respect to the services.
+: Service is active/running
-: Service is inactive/stopped
Let's first start by checking the status of the service. We do this by using the service command with the status action:
This is showing us a lot of information about the service: it's loaded (which means that the OS has the information about the service in memory),
it's enabled (which means that it will start automatically on boot), it's active and running. It also tells us where to find some documentation about
the service and more. Finally, it shows us the last log lines that this service generated.
We get no output from the command. This is a common behavior for many Linux commands. When you ask the system to do something, many
commands will just perform the action without generating any output unless there's an error.
To see the current state, we can query the status of the service again:
We see that the service is now stopped. We can also see what the command logged to /var/log/syslog when finishing:
We can see that nothing was logged, because rsyslog wasn't running. Let's start it back up:
And see that it's running again. Let's try our logger command one more time:
Here you will find - with the cups service, which means it is inactive/stopped. This is the service used to manage printers on Linux systems.
We can get more information about this service by checking the status:
We see here that the cups service is in a failed state. So, let's look at the contents of that directory:
sudo ls -l /etc/cups
content_copy
Output:
There's no cupsd.conf, but there is cupsd.conf.old. Apparently the configuration file was deleted. Good thing we kept a copy! Let's
move that file so that cups can find it and start successfully:
Now that the file was renamed successfully, we can start cups:
Restarting services
Let's go back to the cups service that we just fixed. The logs generated by cups are written into the /var/log/cups directory. We can see the
contents of the directory using the ls command:
sudo ls -l /var/log/cups
content_copy
Output:
The error log file isn't yet created. That's expected because by default cups will only write warning or error messages into that file. If you want
cups to log debug messages into that file, you'll need to change the LogLevel parameter in the configuration file. Let's do that.
If we now restart cups, the service will notice the change to its configuration file. You could do this by using sudo service cups
stop and sudo service cups start, but there is a shortcut.
Reloading Services
Finally, let's look at the reload action. Take this action when you want a service to re-read its configuration without actually doing a full stop and
start.
Let's return the cups log level back to its default. One more time, let's edit /etc/cups/cupsd.conf using the nano editor.
Once you've done this, press "Ctrl-X" to exit the editor. It will ask you if you want to save your changes, press "Y" for yes and then enter at
the filename prompt.
If you check the status of the service, you'll see that it was not restarted (it's been running since we last restarted it).
By using the reload action, we caused the service to re-read its configuration without being stopped at any point.
Conclusion
Congrats! You've successfully listed all the services that are running on the machine, practiced stopping and starting some of these services, and
queried their status. You also fixed a problem in the service that was failing to start and edited the configuration of another service.
These are important commands and problem-solving skills that you'll use on a daily basis as a system administrator. Keep it up!