fedora(linux)创建系统服务 程序开机自启 后台运行

来源:互联网 发布:新开淘宝每天流量100人 编辑:程序博客网 时间:2024/05/18 05:44

fedora可以使用

systemctl start/stop xxx.service 

这样的命令来控制服务
打开目录

cd /etc/systemd/system/

可以看见很多 xxxxxxxx.service 类似的文件 都是可以用 上面的命令控制的
创建一个service文件(可以直接拷贝一个现成的 然后改个名字)
vim打开这个文件 编辑

[Unit]Description=Xxx ServiceAfter=network.target[Service]ExecStart=/xxx/xxx/xxx -c /x/xxxx/xxx/xxx/xxx.xmlRestart=alwaysRestartSec=1[Install]WantedBy=multi-user.target

execstart 填写你需要配置成服务的程序的启动命令(简单的小程序直接书写绝对路径)
保存下来之后
systemctl enable xxx.service
这样这个程序就会随着开机直接启动
可以直接用systemctl start/stop xxx.service 来打开和关闭服务
想要重启这个程序 就上面的命令 中的start改成restart就可以
当然也可以直接killall -9 xxx(你的程序)
想要禁用服务就使用 systemctl disable xxx.service

service配置文件中可以配置的参数有很多不必非要向我上面那么写,有些资料可能会说service的配置文件在/usr/lib/systemd/system/ 目录下,也是对的。但不建议放在该目录下(具体原因这里不与赘述)。

原创粉丝点击