当前位置:网站首页>【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
2022-08-04 20:30:00 【广龙宇】
前言
Service Manager是一个rust的专用跨平台服务管理库。它为rust程序提供了服务管理功能,现在它支持下列服务管理平台的操作接口
sc.exe
WindowsLaunchd
Mac OSsystemd
LinuxOpenRC
Linuxrc.d
FreeBSD
一、安装
在Cargo.toml中添加依赖
service-manager = "0.1"
二、使用例程
通用服务管理
这个 crate 提供了一种机制来检测和使用当前操作系统的默认服务管理平台。每个Service Manager
实例提供四种关键方法:
install
- 安装服务uninstall
- 卸载服务start
- 启动服务stop
- 停止服务
use service_manager::*;
use std::{
ffi::OsString, path::PathBuf};
fn main(){
// 为服务创建一个标签
let label: ServiceLabel = "com.example.my-service".parse().unwrap();
// 通过检测平台上的可用内容获取通用服务
let manager = <dyn ServiceManager>::native()
.expect("Failed to detect management platform");
// 使用底层服务管理平台安装我们的服务
manager.install(ServiceInstallCtx {
label: label.clone(),
program: PathBuf::from("path/to/my-service-executable"),
args: vec![OsString::from("--some-arg")],
}).expect("Failed to install");
// 使用底层服务管理平台启动我们的服务
manager.start(ServiceStartCtx {
label: label.clone()
}).expect("Failed to start");
// 使用底层服务管理平台停止我们的服务
manager.stop(ServiceStopCtx {
label: label.clone()
}).expect("Failed to stop");
// 使用底层服务管理平台卸载我们的服务
manager.uninstall(ServiceUninstallCtx {
label: label.clone()
}).expect("Failed to stop");}
用户级服务管理
默认情况下,服务管理平台会与系统级服务交互;但是,一些服务管理平台喜欢systemd并 launchd支持用户级服务。要在用户级别与服务交互,您可以使用通用 ServiceManager::set_level功能配置您的管理器。
use service_manager::*;
// 创建服务标签
let label: ServiceLabel = "com.example.my-service".parse().unwrap();
// 获取服务
let mut manager = <dyn ServiceManager>::native()
.expect("Failed to detect management platform");
// 设定用户级服务
manager.set_level(ServiceLevel::User)
.expect("Service manager does not support user-level services");
// 其他操作
// ...
特定服务管理
有时您需要对绑定到特定平台的服务的配置进行更多控制。为此,您可以显式创建服务管理器并适当地设置配置属性。
use service_manager::*;
// 创建服务标签
let label: ServiceLabel = "com.example.my-service".parse().unwrap();
// 实例化特定的服务管理器
let mut manager = LaunchdServiceManager::system();
// 更新安装服务时的安装配置属性
// 将不会添加 KeepAlive 标志
manager.config.install.keep_alive = false;
// 使用指定的服务器管理器安装服务
manager.install(ServiceInstallCtx {
label: label.clone(),
program: PathBuf::from("path/to/my-service-executable"),
args: vec![OsString::from("--some-arg")],
}).expect("Failed to install");
总结
以上就是本文的所有内容。本期学习了在Windows平台和Linux平台中,通过 Service Manager库来管理系统的服务,他将成为你后续开发中非常好用的一个工具。
边栏推荐
- 长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析、生物量估算与趋势分析等领域中的应用
- MYSQL gets the table name and table comment of the database
- 密码学系列之:PEM和PKCS7,PKCS8,PKCS12
- 数字IC设计中基本运算的粗略的延时估计
- 大资本已开始逃离加密领域?
- About the state transfer problem of SAP e-commerce cloud Spartacus UI SSR
- QT(42)-QT线程-线程调用槽函数
- Latex分章节、分段落编译:input{}与include{}的区别
- 腾讯云胡启明:Kubernetes云上资源的分析与优化
- 深度解析:为什么跨链桥又双叒出事了?
猜你喜欢
Web3安全风险令人生畏,应该如何应对?
C#移动OA办公系统源码(基于微信企业号)
Five Minutes Introductory Text Processing Three Musketeers grep awk sed
搭建MyCat2一主一从的MySQL读写分离
Oreo域名授权验证系统v1.0.6公益开源版本网站源码
AWS SES 的监控和告警
【Web漏洞探索】跨站脚本漏洞
ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators
漫画 | 老板裁掉我两周后,又把我请回去,工资翻番!
Ant Group's time series database CeresDB is officially open source
随机推荐
C#的Dictionary字典集合按照key键进行升序和降序排列
idea源码无法下载
使用百度EasyDL实现森林火灾预警识别
【数据挖掘】搜狐公司数据挖掘工程师笔试题
37.轮播图
To -.-- -..- -
vs Code 运行一个本地WEB服务器
如何进行AI业务诊断,快速识别降本提效增长点?
Initialization process of SAP UI5
该如何训练好深度学习模型?
c sqlite...
ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
Retrofit的使用及原理详解
Web3安全风险令人生畏,应该如何应对?
The establishment of simple data cache layer
刷题-洛谷-P1307 数字反转
动态规划_双数组字符串
MySQL字段类型
Use "green computing" technology to promote sustainable development of computing power
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex