Systemd 如何设置 开机启动 守护进程

权限:systemd有系统和用户区分;系统(/user/lib/systemd/system/)、用户(/etc/lib/systemd/user/).一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。


1、在/usr/lib/systemd/system/ 目录下 创建对应service(例如我要设置 gogs 为开机启动和守护进程,那么就创建 gogs.service )

 切换目录

cd /lib/systemd/system

创建对应的service

vi gogs.service

编辑 service


[Unit]
Description=Gogs
After=network.target

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=forking
User=gogs
Group=gogs
WorkingDirectory=/usr/local/git/gogs
ExecStart=/bin/sh /usr/local/git/gogs/gogs.sh
Restart=always
Environment=USER=gogs HOME=/home/gogs

# Some distributions may not support these hardening directives. If you cannot start the service due
# to an unknown option, comment out the ones not supported by your version of systemd.
#ProtectSystem=full
#PrivateDevices=yes
#PrivateTmp=yes
#NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

wq 保存退出


2、加载配置,执行配置。

systemctl daemon-reload
systemctl enable gogs.service
systemctl start gogs.service

  查看 sercvice 状态

systemctl status gogs.service

  如果输出如下,那么恭喜你就完成了

 


附上nexus和sonarqube的配置

[Unit]
After=nertwork.target

[Service]
User=sonarqube
Type=forking
#EnvironmentFile=-/etc/profile
ExecStart=/bin/sh -c '. /etc/profile ; /usr/local/java/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh start'
ExecStop=/bin/sh -c '. /etc/profile ; /usr/local/java/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh stop'
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=sonarqube.service
  
[Unit]
After=nertwork.target

[Service]
User=nexus
Type=forking
ExecStart=/bin/sh -c '. /etc/profile ; /usr/local/java/nexus/nexus-3.16.2-01/bin/nexus start'
ExecStop=/bin/sh -c '. /etc/profile ; /usr/local/java/nexus/nexus-3.16.2-01/bin/nexus stop'
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=nexus

具体service 各种配置项 请参考 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值