权限: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