Linux下编写自己的service

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

今天在Linux下源码安装好MySQL后,将mysql添加到系统的服务的过程引起了我的兴趣,我能不能自己写一个简单的脚本,也添加为系统的服务呢?

于是开干:

?
1
2
su
vi  myservice

然后模仿着mysql在里面开写:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
  
start() {
     echo  'This is my service, start command'
     echo  ''
}
stop() {
     echo  'This is my service, stop command'
     echo  ''
}
restart() {
     echo  'This is my service, restart command'
     echo  ''
}
status() {
     echo  'This is my service, status command'
     echo  ''
}
case  "$1"  in 
     start)
         start
         ;;
     stop)
         stop
         ;;
     restart)
         restart
         ;;
     status)
         status
         ;;
     *)
         echo  'Usage: service myservice {start|status|stop|restart}'
         echo  ''
         exit  1
esac
exit  0

很简单,myservice脚本执行时需要接受一个参数,这个参数可以是start, status, stop, restart中间的一个,收到参数后,仅仅做回显使用。写好之后,在拷贝到/etc/init.d/下面去,增加可执行权限,并添加到系统服务:

?
1
2
3
cp  myservice  /etc/init .d /myservice
chmod  +x  /etc/init .d /myservice
chkconfig --add myservice

然后就报错了:

191317_vmwh_1434710.png

google之,发现是chkconfig的注释不能少:

The script must have 2 lines:

# chkconfig: <levels> <start> <stop>
# description: <some description>

之后再打开/etc/init.d/mysql,看看哪里不对,结果发现里面真有这个注释:

184618_U2b6_1434710.png

然后自己也跟着写了个这样的注释,于是脚本就变成了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
 
# For example: following config would generate link
# S51myservice in rc2.d, rc3.d, rc4.d, rc5.d and 
# K49myservice in rc0.d, rc1.d, rc6.d
 
# Comments to support chkconfig on RedHat Linux
# run level, start order, stop order
 
# chkconfig: 2345 51 49 
# description: Customized service written by Alvis.
  
start() {
     echo  'This is my service, start command'
     echo  ''
}
stop() {
     echo  'This is my service, stop command'
     echo  ''
}
restart() {
     echo  'This is my service, restart command'
     echo  ''
}
status() {
     echo  'This is my service, status command'
     echo  ''
}
case  "$1"  in 
     start)
         start
         ;;
     stop)
         stop
         ;;
     restart)
         restart
         ;;
     status)
         status
         ;;
     *)
         echo  'Usage: service myservice {start|status|stop|restart}'
         echo  ''
         exit  1
esac
exit  0

这下好了,再次执行chkconfig:

185028_ih4w_1434710.png

去看看/etc/rc.d/rc2.d,/etc/rc.d/rc3.d,/etc/rc.d/rc4.d,/etc/rc.d/rc5.d下面都生成了什么:

185747_gAyS_1434710.png

可以看到,在rc2.d和rc3.d目录下都生成了一个链接S51myservice,S代表在该运行级别启动,51代表该服务的启动优先级。同理可推,在rc0.d、rc1.d和rc6.d这三个文件夹下会生成K49myservice链接:

185842_9g9O_1434710.png

接下来,可以测试刚刚编写的service 了:

190044_n63D_1434710.png

到这里,简单的service就算是编写完成了。真正的service只是包含了更多的逻辑而已,本质上和这个例子没什么区别。


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
18天前
|
资源调度 JavaScript Linux
【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道
【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道
|
17天前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
17天前
|
存储 Linux 开发工具
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
|
18天前
|
JSON Linux 网络安全
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
|
18天前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
17天前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
17天前
|
JavaScript Linux
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
|
17天前
|
Kubernetes Linux Docker
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
|
18天前
|
Linux Docker 容器
【Azure 应用服务】使用App Service for Linux/Container时,如果代码或Container启动耗时大于了230秒,默认会启动失败。
【Azure 应用服务】使用App Service for Linux/Container时,如果代码或Container启动耗时大于了230秒,默认会启动失败。
|
18天前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?