美文网首页
Systemd简介

Systemd简介

作者: 诺之林 | 来源:发表于2021-01-14 16:22 被阅读0次

    本文的主线 环境 => 服务=> 概念 => 目录 => 体系

    环境

    vagrant up
    
    pipenv run ansible-playbook playbooks/init.yml
    
    pipenv run ansible-playbook playbooks/zsh.yml
    
    pipenv run ansible-playbook playbooks/redis.yml
    
    sudo apt install -y nginx
    

    服务

    ls -l /etc/systemd/system/multi-user.target.wants
    
    cat /etc/systemd/system/multi-user.target.wants/nginx.service
    
    [Unit]
    Description=A high performance web server and a reverse proxy server
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
    ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
    ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
    ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed
    
    [Install]
    WantedBy=multi-user.target
    
    • Description
    systemctl list-units | grep nginx
    # redis.service loaded active running A high performance web server and a reverse proxy server
    
    • After
    man systemd.unit
    # After=
    # A space-separated list of unit names. Configures ordering dependencies between units
    
    • Type
    man systemd.service
    # Type=
    # Configures the process start-up type for this service unit. One of simple, forking, oneshot, dbus, notify or idle
    # If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up
    
    • PIDFile
    man systemd.service
    # PIDFile=
    # Takes an absolute path referring to the PID file of the service. Usage of this option is recommended for services where Type= is set to forking
    
    ps -ef | grep nginx
    
    cat /run/nginx.pid
    
    grep pid /etc/nginx/nginx.conf
    
    • ExecStart & ExecStop
    Linux Kernel
    https://github.com/torvalds/linux/search?q=%22define+SIGTERM%22
    
    SIGTERM vs SIGKILL: What's the Difference?
    https://linuxhandbook.com/sigterm-vs-sigkill/#:~:text=Though%20both%20of%20these%20signals,cannot%20be%20handled%20or%20blocked.
    
    • TimeoutStopSec & Restart
    cat /opt/services/redis/redis-server.pid
    
    sudo killall redis-server
    
    cat /opt/services/redis/redis-server.pid
    
    • Service Unit & Target Unit
    systemctl list-units
    
    systemctl list-units | wc -l
    
    systemctl list-units --type=service
    
    systemctl list-units --type=target
    

    概念

    ps -ef | head -n 2
    # UID        PID  PPID  C STIME TTY          TIME CMD
    # root         1     0  0 17:21 ?        00:00:01 /sbin/init
    
    vim fork.c
    
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int main()
    {
        printf("start\n");
    
        pid_t pid;
        pid = fork();
    
        switch (pid)
        {
        case -1:
            return 1;
    
        case 0:
            printf("this is the child\n");
            break;
    
        default:
            printf("this is the parent\n");
            break;
        }
    
        printf("sleep\n");
        int m = 300;
        while (m--)
        {
            sleep(1);
        }
    
        return 0;
    }
    
    gcc fork.c -o fork
    
    ./fork &
    # start
    # this is the parent
    # sleep
    # this is the child
    # sleep
    
    ps -ef | grep fork
    
    现在 过去
    交互 service service
    实现 systemctl init | systemv
    手册 man init man systemd
    man systemd
    # systemd is a system and service manager for Linux operating systems. When run as first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services.
    # For compatibility with SysV, if systemd is called as init and a PID that is not 1
    

    目录

    ls -l /etc/systemd/system/multi-user.target.wants/nginx.service
    # lrwxrwxrwx 1 root root 33 Jan 14 15:16 /etc/systemd/system/multi-user.target.wants/nginx.service -> /lib/systemd/system/nginx.service
    
    sudo systemctl disable nginx.service
    
    ls -l /etc/systemd/system/multi-user.target.wants/
    
    sudo systemctl enable nginx.service
    
    ls -l /etc/systemd/system/multi-user.target.wants/
    

    体系

    image.png
    sudo journalctl
    sudo journalctl -u nginx.service -f
    
    timedatectl
    timedatectl set-timezone Asia/Shanghai
    
    hostnamectl
    
    localectl
    
    loginctl list-sessions
    loginctl list-users
    
    systemd-analyze blame
    
    sudo systemctl reboot
    
    sudo apt install -y apt-file
    
    apt-file update
    
    apt-file search timedatectl
    
    apt-file search systemctl
    

    参考

    相关文章

      网友评论

          本文标题:Systemd简介

          本文链接:https://www.haomeiwen.com/subject/dsxraktx.html