在CentOS中,管理和控制服務和進程主要通過以下幾種方式:
systemctl
命令systemctl
是CentOS 7及更高版本中用于管理系統服務的命令行工具。
sudo systemctl start <service_name>
sudo systemctl stop <service_name>
sudo systemctl restart <service_name>
sudo systemctl status <service_name>
sudo systemctl enable <service_name>
sudo systemctl disable <service_name>
service
命令service
命令是CentOS 6及更低版本中用于管理服務的主要工具。
sudo service <service_name> start
sudo service <service_name> stop
sudo service <service_name> restart
sudo service <service_name> status
ps
命令查看進程ps
命令用于顯示當前系統中的進程信息。
ps aux
ps aux | grep <process_name>
ps aux | grep <username>
top
命令實時監控進程top
命令提供了一個實時的視圖,顯示系統中的進程及其資源使用情況。
top
htop
命令(高級版本)htop
是top
的一個增強版本,提供了更友好的用戶界面和更多的功能。
htop
sudo yum install htop
htop
htop
kill
命令終止進程如果需要終止某個進程,可以使用kill
命令。
sudo kill <PID>
sudo kill -9 <PID>
systemd
服務文件對于自定義服務,可以在/etc/systemd/system/
目錄下創建服務文件。
sudo vi /etc/systemd/system/my_service.service
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/bin/my_service
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl enable my_service
sudo systemctl start my_service
通過這些工具和方法,你可以有效地管理和控制CentOS系統中的服務和進程。