by Justin EllingwoodIntroduction
Due to its heavy adoption, familiarizing yourself with In this guide, we will be discussing the Please note that although Service ManagementThe fundamental purpose of an init system is to initialize the components that must be started after the Linux kernel is booted (traditionally known as "userland" components). The init system is also used to manage services and daemons for the server at any point while the system is running. With that in mind, we will start with some simple service management operations. In For service management tasks, the target unit will be service units, which have unit files with a suffix of Starting and Stopping ServicesTo start a
As we mentioned above,
Although you may use the above format for general administration, for clarity, we will use the To stop a currently running service, you can use the
Restarting and ReloadingTo restart a running service, you can use the
If the application in question is able to reload its configuration files (without restarting), you can issue the
If you are unsure whether the service has the functionality to reload its configuration, you can issue the
Enabling and Disabling ServicesThe above commands are useful for starting or stopping commands during the current session. To tell
This will create a symbolic link from the system's copy of the service file (usually in To disable the service from starting automatically, you can type:
This will remove the symbolic link that indicated that the service should be started automatically. Keep in mind that enabling a service does not start it in the current session. If you wish to start the service and enable it at boot, you will have to issue both the Checking the Status of ServicesTo check the status of a service on your system, you can use the
This will provide you with the service state, the cgroup hierarchy, and the first few log lines. For instance, when checking the status of an Nginx server, you may see output like this:
This gives you a nice overview of the current status of the application, notifying you of any problems and any actions that may be required. There are also methods for checking for specific states. For instance, to check to see if a unit is currently active (running), you can use the
This will return the current unit state, which is usually
This will output whether the service is A third check is whether the unit is in a failed state. This indicates that there was a problem starting the unit in question:
This will return System State OverviewThe commands so far have been useful for managing single services, but they are not very helpful for exploring the current state of the system. There are a number of Listing Current UnitsTo see a list of all of the active units that
This will show you a list of all of the units that
The output has the following columns:
Since the
We can tell
This will show any unit that You can use other flags to filter these results. For example, we can use the
Another common filter is the
Listing All Unit FilesThe
Units are representations of resources that
The state will usually be "enabled", "disabled", "static", or "masked". In this context, static means that the unit file does not contain an "install" section, which is used to enable a unit. As such, these units cannot be enabled. Usually, this means that the unit performs a one-off action or is used only as a dependency of another unit and should not be run by itself. We will cover what "masked" means momentarily. Unit ManagementSo far, we have been working with services and displaying information about the unit and unit files that Displaying a Unit FileTo display the unit file that
The output is the unit file as known to the currently running Displaying DependenciesTo see a unit's dependency tree, you can use the
This will display a hierarchy mapping the dependencies that must be dealt with in order to start the unit in question. Dependencies, in this context, include those units that are either required by or wanted by the units above it.
The recursive dependencies are only displayed for To show reverse dependencies (units that depend on the specified unit), you can add the Checking Unit PropertiesTo see the low-level properties of a unit, you can use the
If you want to display a single property, you can pass the
Masking and Unmasking UnitsWe saw in the service management section how to stop or disable a service, but
This will prevent the Nginx service from being started, automatically or manually, for as long as it is masked. If you check the
If you attempt to start the service, you will see a message like this:
To unmask a unit, making it available for use again, simply use the
This will return the unit to its previous state, allowing it to be started or enabled. Editing Unit FilesWhile the specific format for unit files is outside of the scope of this tutorial, The
This will be a blank file that can be used to override or add directives to the unit definition. A directory will be created within the Within this directory, a snippet will be created called If you wish to edit the full unit file instead of creating a snippet, you can pass the
This will load the current unit file into the editor, where it can be modified. When the editor exits, the changed file will be written to To remove any additions you have made, either delete the unit's
To remove a full modified unit file, we would type:
After deleting the file or directory, you should reload the
Adjusting the System State (Runlevel) with TargetsTargets are special unit files that describe a system state or synchronization point. Like other units, the files that define targets can be identified by their suffix, which in this case is This can be used in order to bring the system to certain states, much like other init systems use runlevels. They are used as a reference for when certain functions are available, allowing you to specify the desired state instead of the individual units needed to produce that state. For instance, there is a Getting and Setting the Default TargetThe
If you wish to set a different default target, you can use the
Listing Available TargetsYou can get a list of the available targets on your system by typing:
Unlike runlevels, multiple targets can be active at one time. An active target indicates that
Isolating TargetsIt is possible to start all of the units associated with a target and stop all units that are not part of the dependency tree. The command that we need to do this is called, appropriately, For instance, if you are operating in a graphical environment with You may wish to take a look at the dependencies of the target you are isolating before performing this procedure to ensure that you are not stopping vital services:
When you are satisfied with the units that will be kept alive, you can isolate the target by typing:
Using Shortcuts for Important EventsThere are targets defined for important events like powering off or rebooting. However, For instance, to put the system into rescue (single-user) mode, you can just use the
This will provide the additional functionality of alerting all logged in users about the event. To halt the system, you can use the
To initiate a full shutdown, you can use the
A restart can be started with the
These all alert logged in users that the event is occurring, something that simply running or isolating the target will not do. Note that most machines will link the shorter, more conventional commands for these operations so that they work properly with For example, to reboot the system, you can usually type:
ConclusionBy now, you should be familiar with some of the basic capabilities of the While
0 Comments
Leave a Reply. |
Archives
March 2024
Categories
All
|