CentOS7创建个人系统启动服务项的方法

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 

点滴珍贵,重在积累,愿时光慢一点,再慢一点。
原文地址:https://www.cnblogs.com/timemachine213/p/15059191.html