systemd - 如何为所有用户启用、禁用和重新加载 systemd 单元和服务?
问题描述
我正在编写一个包含 systemd 单元的软件包。安装后必须为所有用户启用其单元,删除后必须禁用它。
怎么做?
解决方案
有一个讨论在用户模式下将其实现为“daemon-reexec”。
同时,这是有效的:
#! /bin/bash
Service="foo"
post_install () {
if [[ "$(whoami)" == "root" ]]; then
systemctl --user --global enable "${Service}"
CommandForAllUsers systemctl --user start "${Service}"
fi
}
pre_remove () {
if [[ "$(whoami)" == "root" ]]; then
CommandForAllUsers systemctl --user stop "${Service}"
systemctl --user --global disable "${Service}"
fi
}
post_remove () {
if [[ "$(whoami)" == "root" ]]; then
CommandForAllUsers systemctl --user daemon-reload
fi
}
CommandAsUser () {
local user="${1}"
local command="${*:2}"
local userId="$(id --user "${user}")"
local bus="unix:path=/run/user/${userId}/bus"
local sudoAsUser="sudo -u ${user} DBUS_SESSION_BUS_ADDRESS=${bus}"
${sudoAsUser} ${command}
}
CommandForAllUsers () {
local command="${*}"
local users; readarray -t users <<< "$(loginctl --no-legend list-users | awk '{print $2;}')"
for user in "${users[@]}"; do
CommandAsUser "${user}" "${command}"
done
}
推荐阅读
- android - 房间持久性库中的关系
- c++ - 无法使用柯南在 cmake 中找到请求的 Boost 库
- python - 如何解决 python 中的凯撒密码问题?
- c - 为什么 if( -8 & 7) 返回 false
- python - AttributeError:“str”对象没有属性“get”ocr
- java - 如何从 libgdx 中的像素图中获取每个像素的 int 值
- c# - 如何正确实现状态模式?
- datagrip - 元数据库 - 如何从多个数据库创建一个查询
- css - 选择器中的 SCSS 排除
- python - 使用 python abspath 获得的路径打开 Excel 工作簿