当前位置:   article > 正文

systemctl/systemd 常用命令_/lib/systemd/systemd-logind

/lib/systemd/systemd-logind

01.检查systemd是否正在运行。

  1. # ps -eaf | grep [s]ystemd
  2. root 1 0 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
  3. root 444 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-journald
  4. root 469 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-udevd
  5. root 555 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-logind
  6. dbus 556 1 0 16:27 ? 00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

注意:systemd作为父守护进程运行(PID = 1)。 在上面的命令ps中使用(-e)选择所有进程,( - a)选择除会话前导之外的所有进程和(-f)选择完整格式列表(即-eaf)。

02.列出所有可用的单位

  1. # systemctl list-unit-files
  2. UNIT FILE STATE
  3. proc-sys-fs-binfmt_misc.automount static
  4. dev-hugepages.mount static
  5. dev-mqueue.mount static
  6. proc-sys-fs-binfmt_misc.mount static
  7. sys-fs-fuse-connections.mount static
  8. sys-kernel-config.mount static
  9. sys-kernel-debug.mount static
  10. tmp.mount disabled
  11. brandbot.path disabled
  12. .....

03、检查单元(cron.service)是否启用?

  1. # systemctl is-enabled crond.service
  2. enabled

04.检查单元或服务是否正在运行?

  1. systemctl status firewalld.service
  2. firewalld.service - firewalld - dynamic firewall daemon
  3. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
  4. Active: active (running) since Tue 2018-04-28 16:27:55 IST; 34min ago
  5. Main PID: 549 (firewalld)
  6. CGroup: /system.slice/firewalld.service
  7. └─549 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
  8. Apr 28 16:27:51 tecmint systemd[1]: Starting firewalld - dynamic firewall daemon...
  9. Apr 28 16:27:55 tecmint systemd[1]: Started firewalld - dynamic firewall daemon.

05.列出所有服务(包括启用和禁用)

  1. # systemctl list-unit-files --type=service
  2. UNIT FILE STATE
  3. arp-ethers.service disabled
  4. auditd.service enabled
  5. autovt@.service disabled
  6. blk-availability.service disabled
  7. brandbot.service static
  8. collectd.service disabled
  9. console-getty.service disabled
  10. console-shell.service disabled
  11. cpupower.service disabled
  12. crond.service enabled
  13. dbus-org.fedoraproject.FirewallD1.service enabled

06.如何在Linux中启动,重新启动,停止,重新加载和检查服务(httpd.service)的状态

  1. # systemctl start httpd.service
  2. # systemctl restart httpd.service
  3. # systemctl stop httpd.service
  4. # systemctl reload httpd.service
  5. # systemctl status httpd.service
  6. httpd.service - The Apache HTTP Server
  7. Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
  8. Active: active (running) since Tue 2018-04-28 17:21:30 IST; 6s ago
  9. Process: 2876 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  10. Main PID: 2881 (httpd)
  11. Status: "Processing requests..."
  12. CGroup: /system.slice/httpd.service
  13. ├─2881 /usr/sbin/httpd -DFOREGROUND
  14. ├─2884 /usr/sbin/httpd -DFOREGROUND
  15. ├─2885 /usr/sbin/httpd -DFOREGROUND
  16. ├─2886 /usr/sbin/httpd -DFOREGROUND
  17. ├─2887 /usr/sbin/httpd -DFOREGROUND
  18. └─2888 /usr/sbin/httpd -DFOREGROUND
  19. Apr 28 17:21:30 tecmint systemd[1]: Starting The Apache HTTP Server...
  20. Apr 28 17:21:30 tecmint httpd[2881]: AH00558: httpd: Could not reliably determine the server's fully q...ssage
  21. Apr 28 17:21:30 tecmint systemd[1]: Started The Apache HTTP Server.
  22. Hint: Some lines were ellipsized, use -l to show in full.

注意:当我们使用systemctl等启动,重启,停止和重载等命令时,我们将不会在终端上获得任何输出,只有status命令会打印输出。

07.如何在引导时激活服务并启用或禁用服务(系统引导时自动启动服务)

  1. # systemctl is-active httpd.service
  2. # systemctl enable httpd.service
  3. # systemctl disable httpd.service

实战

创建一个systemd服务

cd /etc/systemd/system

sudo nano ss2.service

ss2.service内容如下

  1. [Unit]
  2. Description=ss2 service
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/bin/bash /root/sh/ss2.sh
  7. [Install]
  8. WantedBy=multi-user.target

systemctl enable ss2

systemctl start ss2

小贴士:

Service types

There are several different start-up types to consider when writing a custom service file. This is set with the Type= parameter in the [Service] section:

  • Type=simple (default): systemd considers the service to be started up immediately. The process must not fork. Do not use this type if other services need to be ordered on this service, unless it is socket activated.
  • Type=forkingsystemd considers the service started up once the process forks and the parent has exited. For classic daemons use this type unless you know that it is not necessary. You should specify PIDFile= as well so systemd can keep track of the main process.
  • Type=oneshot: this is useful for scripts that do a single job and then exit. You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited.
  • Type=notify: identical to Type=simple, but with the stipulation that the daemon will send a signal to systemd when it is ready. The reference implementation for this notification is provided by libsystemd-daemon.so.
  • Type=dbus: the service is considered ready when the specified BusName appears on DBus's system bus.
  • Type=idlesystemd will delay execution of the service binary until all jobs are dispatched. Other than that behavior is very similar to Type=simple.

See the systemd.service(5) § OPTIONS man page for a more detailed explanation of the Type values.

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号