Lesson 7 - Managing Services and Processes on Ubuntu

28/06/2024 - 3 phút

Follow  on Google News

In this lesson, we will learn how to manage services and processes in the Linux system. Managing services and processes is an important part of maintaining stable and efficient system operations. We will learn how to use commands like systemctl, service to manage services and commands like ps, top, htop, and kill to manage processes.

1. Service Management

systemctl: System Service Management

The systemctl command is the main tool for managing services in systems using systemd.

CommandDescriptionExample
systemctl startStart a servicesudo systemctl start nginx
systemctl stopStop a servicesudo systemctl stop nginx
systemctl restartRestart a servicesudo systemctl restart nginx
systemctl enableEnable a service to start automatically on bootsudo systemctl enable nginx
systemctl disableDisable a service from starting automatically on bootsudo systemctl disable nginx
systemctl statusCheck the status of a servicesudo systemctl status nginx

Detailed examples:

  1. Install Nginx:

    sudo apt update
    sudo apt install nginx
    
  2. Start Nginx:

    sudo systemctl start nginx
    
  3. Check the status of Nginx:

    sudo systemctl status nginx
    

    The result will display the current status of the Nginx service, including information about PID, runtime, and error messages if any.

  4. Stop Nginx:

    sudo systemctl stop nginx
    
  5. Restart Nginx:

    sudo systemctl restart nginx
    
  6. Enable Nginx to start automatically when the system boots:

    sudo systemctl enable nginx
    
  7. Disable Nginx from starting automatically when the system boots:

    sudo systemctl disable nginx
    

service: Traditional Service Management

The service command is used to manage services on systems not using systemd.

CommandDescriptionExample
service startStart a servicesudo service nginx start
service stopStop a servicesudo service nginx stop
service restartRestart a servicesudo service nginx restart
service statusCheck the status of a servicesudo service nginx status

Detailed examples:

  1. Start Nginx:

    sudo service nginx start
    
  2. Check the status of Nginx:

    sudo service nginx status
    
  3. Stop Nginx:

    sudo service nginx stop
    
  4. Restart Nginx:

    sudo service nginx restart
    

2. Process Management

ps: Display Running Processes

The ps command is used to display running processes in the system.

CommandDescriptionExample
psDisplay current user’s processesps
ps auxDisplay all running processesps aux
ps -efDisplay processes in full formatps -ef

Detailed examples:

  1. Display current user’s processes:

    ps
    
  2. Display all running processes:

    ps aux
    
  3. Display processes in full format:

    ps -ef
    

top: Real-Time Process Monitoring

The top command provides an interactive interface for monitoring running processes in real-time.

CommandDescriptionExample
topDisplay running processes in real-timetop

Detailed examples:

  1. Run top:

    top
    

    The top interface will display a list of running processes in real-time, including information about CPU, memory, and runtime. You can use shortcuts like q to exit, k to kill a process, and r to change the priority of a process.

htop: Advanced Process Monitoring Interface

The htop command is an advanced version of top with a more user-friendly interface.

CommandDescriptionExample
htopDisplay running processes in real-time with an advanced interfacehtop

Detailed examples:

  1. Install htop:

    sudo apt install htop
    
  2. Run htop:

    htop
    

    The htop interface will display a list of running processes with an advanced graphical interface, allowing you to easily monitor and manage processes. You can use shortcuts like F10 to exit, F9 to kill a process, and F2 to configure htop.

kill: Stop a Process

The kill command is used to stop a running process.

CommandDescriptionExample
killSend a signal to stop a processkill PID
kill -9Send the SIGKILL signal to immediately stop a processkill -9 PID

Detailed examples:

  1. Display a list of running processes:

    ps aux
    

    Find the Process ID (PID) of the process you want to stop.

  2. Stop the process:

    kill 1234
    

    This command will stop the process with ID 1234.

  3. Immediately stop the process:

    kill -9 1234
    

    This command will send the SIGKILL signal to immediately stop the process with ID 1234.

Conclusion

Through this lesson, you have mastered how to manage services and processes in the Linux system. These commands are important tools to help you maintain and operate the system efficiently. I wish you successful practice and application of this knowledge in your daily work.