• Postgresql systemctl设置开机自启动


    Postgresql 数据库需要打开和关闭,一般我们都使用 pg_ctl 命令来进行,实际上在Linux 上系统中为了方便我们也可以通过使用 systemctl 的方式来启动和关闭以及操纵一些相关的功能。

    systemctl 的启动文件,一般存放在/etc/systemd/system 文件夹下,文件的模块主要分为3个。

    1. Unit

    2.  Service

    3.  Install 

    通过 systemctl 命令来对启动文件的加载,起用, 和命令的执行进行执行和管理。
     

    1.在/etc/systemd/system目录下新建配置文件 postgresql.service,内容如下

    [Unit]

    # 简短描述
    Description=postgresql.service  

    # 在network.target服务之后运行,这里可以不要
    After=network.target

    [Service]
    Type=forking
    # 服务的类型,常用的有 simple(默认类型) 和 forking。默认的 simple 类型可以适应于绝大多数的场景,因此一般可以忽略这个参数的配置。而如果
    服务程序启动后会通过 fork 系统调用创建子进程,然后关闭应用程序本身进程的情况,则应该将 Type 的值设置为 forking,否则 systemd 将不会跟踪>子进程的行为,而认为服务已经退出。 系统调用pg后会创建子进程,然后关闭本身进程,所以这里选择forKing进行子进程跟踪
    #运行程序的用户和群组
    User=postgres      
    Group=postgres
    # 工作目录
    WorkingDirectory=/var/lib/pgsql
    # 启动命令
    ExecStart=/usr/bin/pg_ctl start -D /var/lib/pgsql/data
    # 重新加载
    ExecReload=/usr/bin/pg_ctl restart -D /var/lib/pgsql/data
    # 停止程序
    ExecStop=/usr/bin/pg_ctl stop -D /var/lib/pgsql/data
    # 是否给服务分配独立的临时空间,需要
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    #和前面的 Wants 作用相似,只是后面列出的不是服务所依赖的模块,而是依赖当前服务的模块。

     2. 重载systemctl服务

    @localhost ~]$ sudo systemctl daemon-reload 

    3. 启动postgresql服务

    @localhost ~]$ sudo systemctl start postgresql.service

    4.判断服务是否启动,这里提示成功启动

    1. @localhost ~]$ systemctl is-active postgresql.service
    2. active

    5. 也可以通过此命令判断服务状态

    1. @localhost ~]$ sudo systemctl status postgresql.service
    2. ● postgresql.service
    3. Loaded: loaded (/etc/systemd/system/postgresql.service; disabled; vendor preset: disabled)
    4. Active: active (running) since Wed 2022-07-06 09:25:58 CST; 32min ago
    5. Process: 8356 ExecStart=/usr/bin/pg_ctl start -D /var/lib/pgsql/data (code=exited, status=0/SUCCESS)
    6. Main PID: 8358 (postgres)
    7. Tasks: 7 (limit: 47227)
    8. Memory: 20.7M
    9. CGroup: /system.slice/postgresql.service
    10. ├─8358 /usr/bin/postgres -D /var/lib/pgsql/data
    11. ├─8360 postgres: checkpointer process
    12. ├─8361 postgres: writer process
    13. ├─8362 postgres: wal writer process
    14. ├─8363 postgres: autovacuum launcher process
    15. ├─8364 postgres: stats collector process
    16. └─8365 postgres: bgworker: logical replication launcher

    6. 停止postgresql服务

    localhost ~]$ sudo systemctl stop postgresql.service

    7. 如果启动配置失败,也可以通过journalctl命令查看日志查找原因

    localhost ~]$ journalctl -u postgresql.service 
    

    8. 设置开机自启动

    1. [localhost ~]$ sudo systemctl enable postgresql.service
    2. Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /etc/systemd/system/postgresql.service.

    9. systemctl配置文件 

    [Unit]区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系。它的主要字段如下。

    • Description:简短描述
    • Documentation:文档地址
    • Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败
    • Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败
    • BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
    • Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
    • After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
    • Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
    • Condition...:当前 Unit 运行必须满足的条件,否则不会运行
    • Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败

    [Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下。

    • WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
    • RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
    • Alias:当前 Unit 可用于启动的别名
    • Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

    [Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。

    • Type:定义启动时的进程行为。它有以下几种值。
    • Type=simple:默认值,执行ExecStart指定的命令,启动主进程
    • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
    • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
    • Type=dbus:当前服务通过D-Bus启动
    • Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
    • Type=idle:若有其他任务执行完毕,当前服务才会运行
    • ExecStart:启动当前服务的命令
    • ExecStartPre:启动当前服务之前执行的命令
    • ExecStartPost:启动当前服务之后执行的命令
    • ExecReload:重启当前服务时执行的命令
    • ExecStop:停止当前服务时执行的命令
    • ExecStopPost:停止当其服务之后执行的命令
    • RestartSec:自动重启当前服务间隔的秒数
    • Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-successon-failureon-abnormalon-aborton-watchdog
    • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
    • Environment:指定环境变量
  • 相关阅读:
    双指针算法的实现
    OpenCV 05(图像的算术与位运算)
    JavaScript基础知识11——运算符:赋值运算符
    三层架构的学习和实例
    Linux 文件访问权限说明
    ROS+Pytorch的联合使用示例(语义分割)
    基于Spring Boot的智能分析平台
    解决LabVIEW通过OPC Server读取PLC地址时的错误180121602
    3、Nio源码
    解决flume监听文件夹,上传文件时出现java.nio.charset.MalformedInputException: Input length = 1
  • 原文地址:https://blog.csdn.net/weixin_44863237/article/details/125633109