Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Manage Services in Linux Using Systemd

Service management is a crucial aspect of system administration, ensuring that essential services are running, stopped, or restarted as needed. In the Linux environment, systemd has become the standard for service management on most modern distributions. This article will guide you through the basics of managing services using systemd, including starting, stopping, enabling, and disabling services. Understanding how to manage services effectively can help maintain system stability and performance.

Examples:

  1. Checking the Status of a Service: To check the status of a service, use the systemctl status command followed by the service name. For example, to check the status of the apache2 service:

    sudo systemctl status apache2

    This command provides detailed information about the service, including whether it is active, inactive, or failed.

  2. Starting a Service: To start a service, use the systemctl start command followed by the service name. For example, to start the apache2 service:

    sudo systemctl start apache2
  3. Stopping a Service: To stop a service, use the systemctl stop command followed by the service name. For example, to stop the apache2 service:

    sudo systemctl stop apache2
  4. Restarting a Service: To restart a service, use the systemctl restart command followed by the service name. For example, to restart the apache2 service:

    sudo systemctl restart apache2
  5. Enabling a Service: To enable a service to start automatically at boot, use the systemctl enable command followed by the service name. For example, to enable the apache2 service:

    sudo systemctl enable apache2
  6. Disabling a Service: To disable a service from starting automatically at boot, use the systemctl disable command followed by the service name. For example, to disable the apache2 service:

    sudo systemctl disable apache2
  7. Viewing All Services: To list all services and their statuses, use the systemctl list-units --type=service command:

    systemctl list-units --type=service
  8. Creating a Custom Service: To create a custom service, you need to create a service unit file in the /etc/systemd/system/ directory. For example, to create a custom service named mycustom.service, create a file named mycustom.service with the following content:

    [Unit]
    Description=My Custom Service
    
    [Service]
    ExecStart=/usr/bin/mycustomscript.sh
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

    After creating the file, reload the systemd daemon to recognize the new service:

    sudo systemctl daemon-reload

    Then, you can start and enable the custom service as shown in the previous examples.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.