使用Systemd把自作脚本服务化(加入开机启动)

Systemd的出现,使得自己编写的脚本可更容易的添加进系统服务,进而实现开机启动。

例如,我们可以把最简单的hello world脚本进行扩展,将其添加进系统服务,使之开机自启动。

1、编写脚本

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

sudo vim /opt/hello.sh
/opt/hello.sh
#!/bin/bash
while true
do
   echo hello world >> /tmp/hello.log
   sleep 1
done

赋予执行权限。

sudo chmod 0755 /opt/hello.sh

2、在/etc/systemd/system/ 下创建Unit定义文件

sudo vim /etc/systemd/system/hello.service

内容如下

/etc/systemd/system/hello.service
[Unit]
Description = hello daemon

[Service]
ExecStart = /opt/hello.sh
Restart = always
Type = simple

[Install]
WantedBy = multi-user.target

ExecStart中填写想要执行的脚本。
Restart = always 是指进程或服务意外故障的时候可以自动重启的模式。

※Unit文件的详细写法会另外给出。

(Type = simple 指默认的选项没有必要填写,或可理解成其余选项均为系统默认。)

3、把Unit添加进Service

使用systemctl list-unit-files --type=service命令,出现如下图所示即为正常。

$ sudo systemctl list-unit-files --type=service | grep hello
hello.service                              disabled

OK!

4、enable服务后使之start

之后系统将以一般服务的形式对待它。

# 开机自启动on
$ sudo systemctl enable hello
# 单次启动
$ sudo systemctl start hello

运行状态确认

$ sudo systemctl status hello

hello.service - hello daemon
   Loaded: loaded (/etc/systemd/system/hello.service; enabled)
   Active: active (running) since 2018-05-19 09:02:19 UTC; 2min 54s ago
 Main PID: 551 (hello.sh)
   CGroup: /system.slice/hello.service
           ├─ 551 /bin/bash /opt/hello.sh
           └─2062 sleep 1

 6月 19 09:02:19 localhost.localdomain systemd[1]: Started hello daemon.

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

[vagrant@localhost ~]$ tailf /tmp/hello.log
hello world
hello world
hello world
hello world
hello world

成功了!

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

$ sudo reboot

重启后,如正常显示hello服务即为操作政工。

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/100909

(27)
无名无名
上一篇 2018-06-12 15:40
下一篇 2018-06-12 15:45

相关推荐

  • at和crontab命令简述

      at用于计划任务的执行,不过只能执行一次: 1 确定时间 at time 2 确定时刻想要执行的内容 command …     crontab 也用于执行计划任务,可以设定确定的时间多次执行计划任务,时间精确的分钟,可将计划任务按照规定的格式写入配置文件内,配置文件分为六个字段,分别是:分 时 日 月 周 要执…

    Linux笔记 2018-04-08
  • Linux程序包管理

    Linux程序包管理 rpm rpm命令是RPM软件包的管理工具。rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序 语法 rpm(选项)(参数) rpm {-i|–install} [install-options] PACKAGE_FILE … rpm {-U|–upgrade} [install-options] …

    Linux笔记 2018-04-03
  • 搭建完整dns服务(超详细)

    基本框架结构图 域名不一定是magedu.com  可以自定义 准备8台虚拟机 虚拟机地址: 192.168.162.101 作为客户端 192.168.162.102 作为websrv1 端 192.168.162.103 作为websrv2端 192.168.162.104 作为主dns端 192.168.162.105 作为从dns端 192.168.…

    Linux笔记 2018-06-02
  • Linux运维命令以及概念整理总结(3)

    1.用户权限设置以及更改
    2、特殊权限suid、sgid、sticky
    3、ACL权限设置
    4、cat命令、head命令、tail命令、cut命令、paste命令
    5、sort命令、uniq命令、diff命令

    2018-04-14
  • linux网络相关

    网络配置 重启失效 ifconfig 网络地址显示,地址配置 ifconfig 接口 IP/netmask [up|down] [-|+]promisc up 打开 down 关闭 -promisc关闭混杂模式 +promisc打开混杂模式,混杂模式用来监听网络数据 ifconfig eth0:1 ip/netmask route 路由管理命令 查看路由 -…

    Linux笔记 2018-05-06
  • Centos6.9下同时删除/boot下所有文件、/sbin/init文件和/etc/fstab文件的修复方法

    模拟损坏把上述的文件全部删除,启动界面如图所示 重启设置光盘引导后重启 选择第三项 选择ok 选择ok 选择no,不配置网络 选择继续 选择ok 选择ok df查看分区挂载情况,由于/etc/fstab文件被删除,所以看不到根的挂载,使用fdisk –l可以看到分区情况 可以看到一共有五个分区,其中第一个带*表示是启动分区,第四个是扩展分区,第五个是swap…

    2018-05-13