当前位置:网站首页>【一起学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库来管理系统的服务,他将成为你后续开发中非常好用的一个工具。
边栏推荐
- 五分钟入门文本处理三剑客grep awk sed
- Five Minutes Introductory Text Processing Three Musketeers grep awk sed
- Ant Group's time series database CeresDB is officially open source
- WIN10系统如何开启终端
- MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
- 使用 Chrome 开发者工具 coverage 功能分析 web 应用的渲染阻止资源的执行分布情况
- mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
- 新式茶饮,卷完水果还能卷什么?
- 动态数组底层是如何实现的
- 【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
猜你喜欢

Using Baidu EasyDL to realize forest fire early warning and identification

数字IC设计中基本运算的粗略的延时估计

Feign 与 OpenFeign

【数据挖掘】搜狐公司数据挖掘工程师笔试题

【Web漏洞探索】跨站脚本漏洞

QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex

使用百度EasyDL实现森林火灾预警识别

搭建MyCat2双主双从的MySQL读写分离

vscode离线安装插件方法

How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
随机推荐
idea源码无法下载
2022-8-4 第七组 ptz 锁与线程池和工具类
Uniapp微信雪糕刺客单页小程序源码
[Awards for Essays] Autumn recruitment special training to create your exclusive product experience
Latex分章节、分段落编译:input{}与include{}的区别
二叉树的遍历
帝国CMS仿核弹头H5小游戏模板/92game帝国CMS内核仿游戏网整站源码
Unreal 本地化 国家化 多语言
June To -.-- -..- -
Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
run command for node
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
If it is test axi dma catch a few words here
关于 SAP 电商云 Spartacus UI SSR 的 state transfer 问题
明明加了唯一索引,为什么还是产生了重复数据?
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
推荐系统_刘老师
【AGC】构建服务1-云函数示例
ADB 安装 + 打驱动全教程
多用户同时远程登录连接到一台服务器