Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions mxstartup/mxservicectl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ shopt -s nullglob
# exit on any error
set -e

# Run "$@" as a user service of $MX_SRV_USER
#
function run_as_user_service() {
local uid cmd
uid=$(id -u "${MX_SRV_USER}") || return 1
printf -v cmd '%q ' "$@"
systemctl start user@$uid || return 1
su -l "${MX_SRV_USER}" -s /usr/bin/bash -c "XDG_RUNTIME_DIR=/run/user/$uid systemd-run --user --no-ask-password $cmd" || return 1
}

# Run "$@" as user $MX_SRV_USER
# Attempt to run as a user service. If that fails, fall back to running it in the background
#
function run_as_user() {
run_as_user_service "$@" && return 0
local cmd
printf -v cmd '%q ' "$@"
su -l "${MX_SRV_USER}" -s /usr/bin/bash -c "$cmd" &
}

function mxsrv_start_one() {
local cfg=$1
local -i i mip mfwd
Expand All @@ -26,7 +46,7 @@ function mxsrv_start_one() {
systemctl start "${MX_SRV_SCRIPT}" || true
;;
*)
su - ${MX_SRV_USER} -c "${MX_SRV_SCRIPT} start" &
run_as_user ${MX_SRV_SCRIPT} start
;;
esac

Expand All @@ -50,7 +70,7 @@ function mxsrv_stop_one() {
systemctl stop "${MX_SRV_SCRIPT}" || true
;;
*)
su - ${MX_SRV_USER} -c "${MX_SRV_SCRIPT} stop" &
run_as_user ${MX_SRV_SCRIPT} stop
;;
esac

Expand Down