• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
美丽的时光机器
博客园    首页    新随笔    联系   管理    订阅  订阅
CentOS7创建个人系统启动服务项的方法
Linux系统、系统服务

CentOS7.6自定义系统启动项的方法(类似busybox里面的inittab)
1.编写属于自己的unit服务文件,命令为my.service
[Unit]
Description=My-demo Service                                                                                   
[Service]
Type=oneshot
ExecStart=/bin/bash /home/book/workspace/test.sh #自己的脚本文件
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target

2.将上述的文件拷贝到 /usr/lib/systemd/system/目录下

3.编写unit文件中ExecStart=/bin/bash /home/book/workspace/test.sh希望之星的的test.sh文件,将其放在定义的目录当中,文件中可实现我们的自定义操作。
#!/bin/bash                                                                                                                                       
echo "Hello world"

4.将my.service注册到系统当中执行命令:
systemctl enable my.service
输出:ln -s'/usr/lib/systemd/system/my.service' '/etc/systemd/system/multi-user.target.wants/my.service'
注册的过程实际上就是将服务链接到/etc/systemd/system/目录下
重新启动系统,会发现cat /var/log/messages 有Hello woeld输出,表明服务在开机时启动成功。
当然本例当中的test.sh文件可以换成任意的可执行文件作为服务的主体,这样就可以实现各种各样的功能。

5.其他指令:
启动:systemctl start my.service
结束:systemctl stop my.service
重启:systemctl restart my.service
状态:systemctl status my.service
查看网络服务状态: systemctl status network.service
列出所有可用单元:systemctl list-unit-files
列出所有运行中单元:systemctl list-units
列出所有失败单元:systemctl --failed
使用systemctl命令杀死网络服务:systemctl kill network.service 

点滴珍贵,重在积累,愿时光慢一点,再慢一点。
posted on 2021-07-25 22:04  美丽的时光机器  阅读(295)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3