感谢支持
我们一直在努力

Linux systemctl 常用用法简介

主要介绍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

赞(0) 打赏
转载请注明出处:服务器评测 » Linux systemctl 常用用法简介
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏