技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页Linux>正文
Ubuntu Server 20.04.x LTS使用systemd创建自定义开机启动服务
2021年05月05日 Linux 暂无评论 ⁄ 被围观 5,103次+

Ubuntu Server 20.04.x LTS系统中需要开机启动某个服务、开机备份文件等都可以通过systemd实现

1、创建配置文件

nano /lib/systemd/system/autostart.service #创建文件

[Unit]

Description=autostart

[Service]

Type=forking

PIDFile=/run/autostart.pid

#EnvironmentFile=/etc/systemd/autostart.conf

ExecStart=/home/autostart.conf

ExecReload=/bin/kill -SIGHUP $MAINPID

ExecStop=/bin/kill -SIGINT $MAINPID

[Install]

WantedBy=multi-user.target graphical.target

Alias=autostart.service

ctrl+o #保存配置

ctrl+x #退出

systemctl daemon-reload  #重新加载配置

说明:

[Unit] 段: 启动顺序与依赖关系

[Service] 段: 如何启动、启动类型

[Install] 段: 如何实现开机启动

2、添加启动脚本

nano /home/autostart.conf #编辑启动脚本

#!/bin/bash

echo `date`,"ok" >>/home/autostart.log

ctrl+o #保存配置

ctrl+x #退出

chmod +x /home/autostart.conf #添加执行权限

ln -s /lib/systemd/system/autostart.service   /etc/systemd/system/ #创建软连接文件

systemctl enable autostart.service #添加开机启动

journalctl -u autostart.service

重新启动系统之后,会自动启动autostart.service服务,在/home/autostart.log里面可以看到有时间写入。

/home/autostart.conf启动脚本文件里面可以自定义设置,把你想要开机启动的服务配置在这个文件里面。

至此,Ubuntu Server 20.04.x LTS使用systemd创建自定义开机启动服务教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2024 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接
陕ICP备11001040号-3