【linux】使用 systemctl 控制服务开机自启动

【Linux】使用 systemctl 控制服务开机自启动

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

  1. 准备: hello.service && root 权限

【hello.service】

[Unit]
Description=Lanuch hello  Service
After=hello.service

[Service]
Type=idle
Restart=on-failure
WorkingDirectory=/www/hello/bin     #bin文件路径
Environment="LD_LIBRARY_PATH=/usr/lib64:/www/hello/lib"   #还未加载/etc/bash_profile | bin文件需要的环境变量,动态库,系统库
ExecStart=/www/hello/bin/hello      #bin文件
StandardOutput=null                 #重定向日志打印,不设置该项服务日志将会打印到/var/log/message ,该目录一般较小,很容易打满搞挂机器,一定记得设置

[Install]
WantedBy=multi-user.target
  1. 加入 /etc/systemd/system/hello.service

  2. 刷新systemctl 控制服务
    systemctl daemon-reload 将hello.service 加入systemctl管理

  3. 查看服务状态是否添加成功
    systemctl status hello.service
    在这里插入图片描述

  4. 将服务加入开机自启动
    systemctl enable hello.service
    在这里插入图片描述

  5. 启动 & 停止 服务
    systemctl start hello.service
    systemctl stop hello.service
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值