当前位置:网站首页>【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
2022-08-04 20:30:00 【广龙宇】
前言
Service Manager是一个rust的专用跨平台服务管理库。它为rust程序提供了服务管理功能,现在它支持下列服务管理平台的操作接口
sc.exeWindowsLaunchdMac OSsystemdLinuxOpenRCLinuxrc.dFreeBSD
一、安装
在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库来管理系统的服务,他将成为你后续开发中非常好用的一个工具。
边栏推荐
- Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
- mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
- 动态规划_双数组字符串
- About the state transfer problem of SAP e-commerce cloud Spartacus UI SSR
- 基于单向链表结构的软件虚拟定时器的设计与构建
- Web3安全风险令人生畏,应该如何应对?
- 五分钟入门文本处理三剑客grep awk sed
- Unreal 本地化 国家化 多语言
- KubeSphere简介,功能介绍,优势,架构说明及应用场景
- 二叉树的前序遍历
猜你喜欢
随机推荐
How to use the Chrome DevTools performance tab
数据安全解决方案的发展
【数据挖掘】搜狐公司数据挖掘工程师笔试题
长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析、生物量估算与趋势分析等领域中的应用
推荐系统_刘老师
If it is test axi dma catch a few words here
面试官:JVM运行时数据区包含哪几部分?作用是啥?
C语言基础[通俗易懂]
Embrace the Cmake child is simple and practical, but inflexible
二叉树是否对称
面试官:索引为什么会失效?
C#移动OA办公系统源码(基于微信企业号)
MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
数字IC设计中基本运算的粗略的延时估计
手撕SparkSQL五大JOIN的底层机制
【Web漏洞探索】跨站脚本漏洞
动态数组底层是如何实现的
win10 uwp 使用 ScaleTransform 放大某个元素
实现菜单拖拽排序
【CAS:2306109-91-9 |胺-PEG4-脱硫生物素】价格









