linux 服务开机自启动systemd方式 (Centos7)
1.Centos7 搭建prometheus+Grafana监控2.Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警3.Prometheus中文文档4.Prometheus 通过 consul 实现自动服务发现5.基于Centos7.4搭建prometheus+grafana+altertManger监控Spring Boot微服务
6.linux 服务开机自启动systemd方式 (Centos7)
7.Centos 7.4搭建es7.12.0+Skywalking7.8.58.基于Centos 7.4 搭建ELK整合SpringBoot日志收集9.skywalking 通过python探针监控Python 微服务应用性能10.SkyWalking8.5集成spring-logback.xml日志收集11.python3 实现skywalking调用webhook接口通过python3发送邮件告警12.centos7 python2和python3共存13.filebeat+logstash+elasticsearch+kibana多来源日志收集平台14.kibana启动失败Kibana server is not ready yet,ES后台日志报错:NoShardAvailableActionExceptionlinux 服务开机自启动systemd方式 (Centos7)
1、编写一个 /opt/hello.sh 脚本
1 2 3 4 5 6 7 8 | [root@jws- ftp prometheus] # cat /opt/hello.sh #!/bin/bash while true do echo hello world >> /tmp/hello .log sleep 10 done [root@jws- ftp prometheus] # |
赋予执行权限
1 2 3 4 5 | [root@jws- ftp prometheus] # chmod 0755 /opt/hello.sh [root@jws- ftp prometheus] # [root@jws- ftp prometheus] # ll /opt/hello.sh -rwxr-xr-x. 1 root root 85 3月 1 14:04 /opt/hello .sh [root@jws- ftp prometheus] # |
2、在/etc/systemd/system/ 下创建Unit定义文件
1 2 3 4 5 6 7 8 9 10 11 12 | [root@jws- ftp prometheus] # cat /etc/systemd/system/hello.service [Unit] Description = hello [Service] ExecStart = /bin/bash /opt/hello .sh Restart = always Type = simple [Install] WantedBy = multi-user.target [root@jws- ftp prometheus] # |
ExecStart中填写想要执行的脚本
Restart = always 是指进程或服务意外故障的时候可以自动重启的模式。
※Unit文件的详细写法会另外给出。
(Type = simple 指默认的选项没有必要填写,或可理解成其余选项均为系统默认)
3、把Unit添加进Service
使用systemctl list-unit-files --type=service
命令查看,出现如下图所示即为正常。
1 2 3 | [root@jws- ftp prometheus] # systemctl list-unit-files --type=service | grep hello hello.service disabled [root@jws- ftp prometheus] # |
4、enable服务后使之start
之后系统将以一般服务的形式处理它
1 2 3 4 5 | # 开机自启动on [root@jws- ftp prometheus] # systemctl enable hello # 立即启动 [root@jws- ftp prometheus] # systemctl start hello |
运行状态确认
1 2 3 4 5 6 7 8 9 10 11 12 | [root@jws- ftp prometheus] # systemctl status hello ● hello.service - hello Loaded: loaded ( /etc/systemd/system/hello .service; enabled; vendor preset: disabled) Active: active (running) since 一 2021-03-01 14:12:08 CST; 11min ago Main PID: 38133 ( bash ) CGroup: /system .slice /hello .service ├─38133 /bin/bash /opt/hello .sh └─38272 sleep 10 3月 01 14:12:08 jws- ftp systemd[1]: Started hello. 3月 01 14:12:08 jws- ftp systemd[1]: Starting hello... [root@jws- ftp prometheus] # |
打开日志文件看看脚本是否正常运作
1 2 3 4 5 | [root@jws- ftp prometheus] # tailf /tmp/hello.log hello world hello world hello world hello world |
5、重启机器,查看服务是否正常自动启动
1 | [root@jws- ftp prometheus] # reboot |
重启后,如正常显示hello服务即为操作成功
Systemd的使用指南
https://www.jianshu.com/p/7fd8b6ea336e
spring-boot 项目可以配合 启动脚本进行使用
https://my.oschina.net/zhongwenhao/blog/3064313
tomcat示例:
tomcat.service
1 2 3 4 5 6 7 8 9 10 11 | [Unit] Description=A Servlet Container [Service] Type=forking PIDFile= /usr/local/jenkins/apache-tomcat-7 .0.104 /tomcat .pid ExecStart= /usr/local/jenkins/apache-tomcat-7 .0.104 /bin/catalina .sh start ExecStop= /usr/local/jenkins/apache-tomcat-7 .0.104 /bin/catalina .sh stop [Install] WantedBy=multi-user.target |
本文作者:香菜哥哥
本文链接:https://www.cnblogs.com/yizhipanghu/p/14463432.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步