linux 服务开机自启动systemd方式 (Centos7)

1、编写一个 /opt/hello.sh 脚本

[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]#

赋予执行权限

[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定义文件

[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命令查看,出现如下图所示即为正常。

[root@jws-ftp prometheus]# systemctl list-unit-files --type=service | grep hello
hello.service disabled
[root@jws-ftp prometheus]#

4、enable服务后使之start

之后系统将以一般服务的形式处理它

# 开机自启动on
[root@jws-ftp prometheus]# systemctl enable hello # 立即启动
[root@jws-ftp prometheus]# systemctl start hello

运行状态确认

[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]#

打开日志文件看看脚本是否正常运作

[root@jws-ftp prometheus]# tailf /tmp/hello.log
hello world
hello world
hello world
hello world

5、重启机器,查看服务是否正常自动启动

[root@jws-ftp prometheus]# reboot

重启后,如正常显示hello服务即为操作成功

Systemd的使用指南

https://www.jianshu.com/p/7fd8b6ea336e

spring-boot 项目可以配合 启动脚本进行使用

https://my.oschina.net/zhongwenhao/blog/3064313

linux 服务开机自启动systemd方式 (Centos7)的更多相关文章

  1. linux服务开机自启动&注册系统服务

    首先先看下linux系统开机启动顺序,如下图 对,要解决Linux CentOS 和 Red Hat Linux 系统中设置服务自启动有两种方式,就是从图中圈出的两个步骤下手. 一.修改 /etc/r ...

  2. linux -- 服务开机自启动

    好吧,最近需要用到开机启动服务,百度了一下,几乎都是一个版本,然后之间各种传递.我也抄个 ******************************************************* ...

  3. Linux服务开机自启动设置

    Linux中也有类似于Window中的开机自启动服务,主要是通过chkconfig命令来设置.它主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服 ...

  4. Linux服务开机自启动使用示例

    本文以redis服务为例,介绍了两种服务自启动的方法service,systemctl使用示例 1.修改redis.conf,允许后台运行 daemonize no 改为 daemonize yes ...

  5. Linux(centos7)设置docker服务开机自启动以及容器自启动

    docker服务开机自启动 systemctl enable docker 设置容器自启动 可以在运行的时候通过设置--restart 参数 docker run --restart always - ...

  6. Linux 服务管理两种方式service和systemctl

    Linux 服务管理两种方式service和systemctl 1.service命令 service命令其实是去/etc/init.d目录下,去执行相关程序 # service命令启动redis脚本 ...

  7. linux 添加开机自启动脚本

    原文 Linux设置服务开机自动启动的方式有好多种,这里介绍一下通过chkconfig命令添加脚本为开机自动启动的方法. 1. 编写脚本autostart.sh(这里以开机启动redis服务为例),脚 ...

  8. 如何把一个c语言程序做成windows服务开机自启动

    原文:如何把一个c语言程序做成windows服务开机自启动 目前写的程序是一个用c语言实现socket侦听的,那么如何把这个程序做成开机自启动呢? 我们是通过vs6.0,编译后生成了.exe文件,然后 ...

  9. 红帽linux系统开机自启动脚本。

    其实很多东西在最后完成以后会觉得也就那样,有意思的是探究的过程. 前段时间老板要求把一个程序做成linux系统开机自启动脚本的模式. 首先你需要写一个脚本. 我这边建立了一个.sh的脚本,就是用脚本启 ...

随机推荐

  1. 对不起,“下一代ERP”仍旧是现在的ERP

    最近数字化转型太火了,到处都是相关数字化的网文.很多人又说在数字化转型时代,ERP早就落伍了云云,取而代之的是什么"下一代ERP",叫什么"ARP"." ...

  2. 【剑指offer】9:变态跳台阶

    题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 解题思路: 先考虑最简单情况就是只有一级台阶,仅有一种跳法.两级台阶,有两种 ...

  3. pwnable.tw orw

    orw 首先,检查一下程序的保护机制 开启了canary保护,还是个32位的程序,应该是个简单的题

  4. hibernate的三种查询方式

    hibernate的三种查询方式 目录 hibernate的三种查询方式 1.HQL查询 1.1.SQL概述 1.2.实体查询 1.3.带where的查询 1.3.属性查询 1.4.实体的更新和删除 ...

  5. DPAPI机制学习

    0x00 前言 ​绝大多数应用程序都有数据加密保护的需求,存储和保护私密信息最安全的方式就是每次需要加密或解密时都从用户那里得到密码,使用后再丢弃.这种方式每次处理信息时都需要用户输入口令,对于绝大多 ...

  6. 2021S软件工程——案例分析作业

    2021S软件工程--案例分析作业 18231169 黄思为 项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任建) 这个作业的要求在哪里 案例分析作业 我在这个课程的目标是 了解并熟悉 ...

  7. B - Rikka with Graph HDU - 5631 (并查集+思维)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  8. 从苏宁电器到卡巴斯基第10篇:我在苏宁电器当营业员 II

    之所以是主推,其实是有原因的 据我所知,尽管诺基亚卖的很好,但是他们的厂促的待遇却很一般,估计也就一千多两千的样子,撑死两千多.但是呢,记得当时我们的卖场里面还有联想手机,别看卖得相当次,但是他们的厂 ...

  9. IDAPython类库---idaapi.py的源码

    #ThisfilewasautomaticallygeneratedbySWIG(http://www.swig.org).#Version2.0.12##Donotmakechangestothis ...

  10. hdu1722 切蛋糕

    题意:CakeTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...