首頁 > 軟體

Linux systemctl 常用用法簡介

2020-06-16 17:04:16

主要介紹systemctl的幾個功能如下:

1.檢視某個服務的狀態
2.關閉某個服務
3.開啟某個服務
4.設定某個為開機自啟動
5.關閉某個服務為開機不啟動
6.檢視所有開啟啟動的服務

1.檢視某個服務的狀態

用法:systemctl status 需要查詢的服務 例如 mariadb.service 注意Actice
[root@localhost linux]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
  Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Active: active (running) since Wed 2017-05-10 09:48:44 EDT; 19min ago
  Process: 2203 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1057 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 2198 (mysqld_safe)
  CGroup: /system.slice/mariadb.service
          ├─2198 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
          └─2445 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/...

May 10 09:48:25 localhost.localdomain systemd[1]: Starting MariaDB database server...
May 10 09:48:29 localhost.localdomain mysqld_safe[2198]: 170510 09:48:29 mysqld_safe Logging to '/var/log...g'.
May 10 09:48:29 localhost.localdomain mysqld_safe[2198]: 170510 09:48:29 mysqld_safe Starting mysqld daem...sql
May 10 09:48:44 localhost.localdomain systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost linux]#

其實檢視狀態還可以用ps查詢

ps aux 是查詢所有進程grep是提取需要的行
[root@localhost linux]# ps aux | grep mariadb
mysql    4987  1.2  2.1 839268 83484 ?        Sl  10:11  0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      5022  0.0  0.0 112648  952 pts/1    S+  10:11  0:00 grep --color=auto mariadb
[root@localhost linux]#

2.關閉某個服務

[root@localhost linux]# systemctl stop mariadb.service
[root@localhost linux]#
再次檢視其狀態 可見 已經關閉完成了
[root@localhost linux]# ps aux | grep -v grep | grep mariadb.service
[root@localhost linux]#

3.開啟某個服務

[root@localhost linux]# systemctl start mariadb.service
[root@localhost linux]#
檢視服務是否啟動
[root@localhost linux]# ps aux | grep mariadb
mysql    5247  0.3  2.1 839268 83488 ?        Sl  10:17  0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      5287  0.0  0.0 112648  952 pts/1    S+  10:18  0:00 grep --color=auto mariadb
[root@localhost linux]#
[root@localhost linux]#
[root@localhost linux]# ps aux | grep mariadb | grep -v grep
mysql    5247  0.3  2.1 839268 83488 ?        Sl  10:17  0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

4.設定某個服務為開機自啟動

[root@localhost linux]# systemctl is-enabled mariadb.service
enabled
[root@localhost linux]#

5.關閉某個服務為開機不啟動

[root@localhost linux]# systemctl disable mariadb.service
Removed symlink /etc/systemd/system/multi-user.target.wants/mariadb.service.
[root@localhost linux]#

6.檢視所有開啟啟動的服務

[root@localhost linux]# ls /etc/systemd/system/multi-user.target.wants/
abrt-ccpp.service    auditd.service      kdump.service          postfix.service  sysstat.service
abrtd.service        chronyd.service    libstoragemgmt.service  remote-fs.target  tuned.service
abrt-oops.service    crond.service      mdmonitor.service      rngd.service      vmtoolsd.service
abrt-vmcore.service  cups.path          ModemManager.service    rsyslog.service  vsftpd.service
abrt-xorg.service    cups.service        NetworkManager.service  smartd.service
atd.service          irqbalance.service  nfs-client.target      sshd.service
[root@localhost linux]#

可見,剛剛禁止之後,這個檔案中就沒有了mariadb.service
[root@localhost linux]# ls /etc/systemd/system/multi-user.target.wants/mariadb.service
ls: cannot access /etc/systemd/system/multi-user.target.wants/mariadb.service: No such file or directory
[root@localhost linux]#

現在利用systemctl將mariadb.service設定為開機自啟動
[root@localhost linux]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@localhost linux]#

再次寫ls語句
[root@localhost linux]# ls /etc/systemd/system/multi-user.target.wants/mariadb.service
/etc/systemd/system/multi-user.target.wants/mariadb.service
[root@localhost linux]#

systemctl 命令完全指南  http://www.linuxidc.com/Linux/2015-07/120833.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-10/147886.htm


IT145.com E-mail:sddin#qq.com