写一个简单服务,我们需要一个该完成服务的脚本,还需要一个启动文件,下面以打印hello来作为例子讲解
- 新建一个hello.sh的文件,里面写入如下文本(当然可以自定义的):
#!/bin/bash echo "Hello Hxd! This is a service test! Haha"
再/etc/systemd/system/底下新建一个hello.service文件,写入如下内容:
[Unit] Description=Say Hello for you //说明性语句 [Service] Type=simple //启动的方式为simple ExecStart=/bin/bash /root/hello.sh //该hello.sh路径要根据你的来,我的是在/root/路径下 [Install] WantedBy=muli-user.target //表示该服务附在muli-user.target下
一次输入如下命令:
- 重载配置文件 : systemctl deamon-reload
- 开启服务:systemctl start hello.service
- 查看状态:systemctl status hello.service
我们的输出语句“Hello Hxd! This is a service test! Haha”就在图片下面的提示下面,他不会输出当我们的终端。因为我们只是一个简单地script脚本,所以执行后马上就结束了,古我们看到的是inactive状态