0% found this document useful (0 votes)
8 views

Managing Services On Linux

The document provides reminders about various Linux commands like sudo, ls, mv, tail, cat, grep, less. It also introduces new commands like service, logger. It discusses using service to list, stop, start, reload services. Specifically, it shows stopping and starting the rsyslog service, fixing a failing cups service by moving its configuration file, changing the cups log level by editing its configuration file, and reloading cups to re-read the configuration.

Uploaded by

arunajith897
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Managing Services On Linux

The document provides reminders about various Linux commands like sudo, ls, mv, tail, cat, grep, less. It also introduces new commands like service, logger. It discusses using service to list, stop, start, reload services. Specifically, it shows stopping and starting the rsyslog service, fixing a failing cups service by moving its configuration file, changing the cups log level by editing its configuration file, and reloading cups to re-read the configuration.

Uploaded by

arunajith897
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Linux commands reminder

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:

 sudo <command>: executes a command with administrator rights


 ls <directory>: lists the files in a directory
 mv <old_name> <new_name>: moves or renames a file from the old name to the new name
 tail <file>: shows the last lines of a file
 cat <file>: prints the whole contents of a file
 grep <pattern> <file>: filters the text of a file according to the pattern
 less <file>: lets you browse a file
Additionally, you can combine these commands using the | sign. For example:

sudo cat /var/log/syslog | grep error | tail


content_copy
will first print the output of /var/log/syslog, then keep only the lines that say "error" and then the last 10 lines of that output.

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.

Listing system services

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:

sudo service --status-all


content_copy
Output:

Here you will see the following notations with respect to the services.

 +: Service is active/running

 -: Service is inactive/stopped

 ?: Can't determine whether service is active or not


Stopping and starting services
Alright, now that we've listed the services let's practice stopping and starting some of them. The first service that we are going to stop is
the rsyslog service. This service is in charge of writing content to the log files, as
in /var/log/syslog, /var/log/kern.log, /var/log/auth.log and others. Processes that generate output will send that output to the
rsyslog service and the service will write it to the corresponding log files depending on how the system was configured.

Let's first start by checking the status of the service. We do this by using the service command with the status action:

sudo service rsyslog status


content_copy
Output:

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 can see this service in action by using the logger command:

logger This is a test log entry


content_copy
The logger command will send the text to the rsyslog service and the service will then write it into /var/log/syslog. We can check that this
is the case by looking at the last lines in /var/log/syslog.

sudo tail -1 /var/log/syslog


content_copy
Output:

Let's now go ahead and stop the rsyslog service:

sudo service rsyslog stop


content_copy
We need to execute the command with sudo, because while all users can query the status of services, only users with administrator rights can
stop or start services.

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:

sudo service rsyslog status


content_copy
Output:

We see that the service is now stopped. We can also see what the command logged to /var/log/syslog when finishing:

sudo tail -5 /var/log/syslog


content_copy
Output:
In the last line, we see that the rsyslog service has exited and is no longer running.

We can try sending text with our logger command again:

logger This is another test log entry


content_copy
And then check that the contents of /var/log/syslog:

sudo tail /var/log/syslog


content_copy
Output:

We can see that nothing was logged, because rsyslog wasn't running. Let's start it back up:

sudo service rsyslog start


content_copy
Again, no output, this is expected. We can check the status:

sudo service rsyslog status


content_copy
Output:

And see that it's running again. Let's try our logger command one more time:

logger This is another test log entry


content_copy
And then check that the contents of /var/log/syslog:

sudo tail /var/log/syslog


content_copy
Output:

The message is now there!


Click Check my progress to verify the objective.

Stop and Start rsyslog Service


Check my progress

Fixing a failing service


In order to list the state of services controlled by System V, you can use the following command:

sudo service --status-all


content_copy
Output:

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:

sudo service cups status


content_copy
Output:

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:

sudo mv /etc/cups/cupsd.conf.old /etc/cups/cupsd.conf


content_copy
As with the other commands, we get no output after executing this. We can run ls again to see that the file was renamed correctly:
sudo ls -l /etc/cups
content_copy
Output:

Now that the file was renamed successfully, we can start cups:

sudo service cups start


content_copy
And then check the status:

sudo service cups status


content_copy
Output:

We've fixed it!

Click Check my progress to verify the objective.

Fix Cups Service


Check my progress

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.

Let's edit /etc/cups/cupsd.conf using the nano editor.

sudo nano /etc/cups/cupsd.conf


content_copy
In one of the first lines of the file you'll see there's a line that says LogLevel warn. We want to replace warn with debug:
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 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.

sudo service cups restart


content_copy
Restarting the service command is a handy way of stopping a service and then starting it immediately back up. And once we've done that, we can
see that there's now a lot of content in /var/log/cups/error_log.

sudo cat /var/log/cups/error_log


content_copy
Output:

Click Check my progress to verify the objective.

Change the Log Level for Cups


Check my progress

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.

sudo nano /etc/cups/cupsd.conf


content_copy
Let's change LogLevel debug, replacing debug with warn:

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.

Once we've done this, we can reload cups:

sudo service cups reload


content_copy
Output:

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).

sudo service cups status


content_copy
Output:

By using the reload action, we caused the service to re-read its configuration without being stopped at any point.

Click Check my progress to verify the objective.

Revert the Log Level for Cups


Check my progress

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!

You might also like