linux添加自定义service服务,开机自启动

14 篇文章 0 订阅

自己的服务开机自启动

第一步: 准备配置文件
  • 配置文件示例:xxx.service
[Unit]
Description=你的服务名称 Service
ConditionPathExists=/home/user/install/xxxxxxx
After=network.target

[Service]
Type=simple
User=root
EnvironmentFile=/home/user/install/env // 环境变量, 不需要则置为空
WorkingDirectory=/home/user/install
ExecStart=/home/user/install/xxxxxxx -c /home/user/install/config.yml
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=xxxxxxx

[Install]
WantedBy=multi-user.target

  • ps :env示例
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user/project
第二步:启动服务
  • 命令
sudo cp xxx.service /etc/systemd/system
sudo systemctl start xxx.service
sudo systemctl enable xxx.service
备注
  • 命令
/etc/systemd/system  ### 自定义服务存放路径
systemctl start xxx.service ### 启动
systemctl enable xxx.service 加入开机自启动
systemctl disable xxx.service 移除开机自启动
systemctl start xxx.service 启动
systemctl daemon-reload 修改了文件后重新加载
查询日志
  • 查看指定服务日志 journalctl -u 服务名
  • 查看指定日期日志 journalctl --since="2021-10-10 10:10:00" --until="2021-10-11 10:10:00" -u 服务名
  • 类似tail -f journalctl -f -n 20 -u 服务名
    • -n 查看尾部多少行
    • -f 滚动形式
  • 查看日志占用的磁盘空间 journalctl --disk-usage
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值