site  contact  subhomenews

Script to run an app as any user

February 08, 2026 — BarryK

There was a struggle reported recently on the forum, an app tried to run chromium, but failed.

I can't recall which app, but say there is a an app named "myapp"; if installed to run non-root, it will run as user "myapp". Chromium runs as user "chromium", and it looks like this is the problem.

What will normally happen, is if an app running non-root tries to run chromium, there will be a popup asking for the root password (and then chromium will run, as user chromium). But the problem reported in the forum, this mechanism was not used.

Even though myapp is installed to run as user "myapp", this new script can override, like this:

# run-as chromium myapp

...this will run myapp as user "chromium". This may solve the problem. It will be interesting to see how this gets used.

The generic format is:

# run-as username appname parameters

Here is the content of /usr/local/clients/run-as (with /usr/sbin/run-as symlink) script:

#!/bin/sh
#20260208 wrapper to run an app as a particular user (when currently running as root)
#$1 = username, $2+ = appname and params

export TEXTDOMAIN=run-as
export OUTPUT_CHARSET=UTF-8

case "$1" in
""|-h*)
echo "$(gettext 'Script to run an app as a particular user, when currently running as root')"
echo '$1 = username, $2+ = appname and params'
exit
;;
esac

if [ $(id -u) -ne 0 ]; then
echo "$(gettext 'Must run this script as the root user.')"
exit
fi

xUSER="$1"; shift
grep -q "^${xUSER}:" /etc/passwd
if [ $? -ne 0 ];then
echo "(gettext 'First parameter is not a valid username, in /etc/passwd')"
exit
fi
grep -q "^${xUSER}:" /etc/group
if [ $? -ne 0 ];then
echo "$(gettext 'First parameter is not a valid group name, in /etc/group')"
exit
fi
if [ ! -d /home/${xUSER} ];then
echo "$(gettext 'Directory does not exist:') /home/${xUSER}"
exit
fi

if [ -z "$1" ];then
echo "$(gettext 'Second parameter app name not specified')"
exit
fi

APP="$1"; shift
if [ ! -x /usr/bin/${APP} ];then
echo "$(gettext 'Executable does not exist:') /usr/bin/${APP}"
exit
fi

if [ -x /usr/bin/${APP}.bin0 ];then
xAPP="${APP}.bin0"
elif [ -x /usr/bin/${APP}.bin ];then
xAPP="${APP}.bin"
else
xAPP="${APP}"
fi

ARGS=""
#[ $1 ] && while [ "$1" ]; do ARGS="$ARGS \"$1\""; shift; done
if [ -n "$1" ];then
while [ "$1" ]
do
#do not put quotes around if a single word. example is "-edit" for seamonkey, the quotes stuff it up entirely...
if [ "${1/ /}" == "${1}" ];then
ARGS="${ARGS} ${1}"
else
ARGS="${ARGS} \"${1}\""
fi
shift
done
fi

#this is a method to authorize x server to run by user $xUSER. works when server running.
#note: this setting does not seem to be stored in a file. it affects the currently running
# x server and will be lost when x quits.
ALLOWflg="$(xhost | grep -o ":${xUSER}$")"
if [ ! "$ALLOWflg" ];then
xhost +SI:localuser:${xUSER}
fi

#[ $XAUTHORITY ] && cp $XAUTHORITY /home/${xUSER}/.Xauthority 2>/dev/null
#touch /home/${xUSER}/.Xauthority

#following line is mostly there to catch any root:root files that may have got copied in...
#chown -R ${xUSER}:${xUSER} /home/${xUSER} &
#sleep 0.1

export XAUTHORITY=/home/${xUSER}/.Xauthority
export XDG_CONFIG_HOME=/home/${xUSER}/.config
export XDG_CACHE_HOME=/home/${xUSER}/.cache
export XDG_DATA_HOME=/home/${xUSER}/.local/share

###
exec su -l ${xUSER} -s /bin/sh -c "DISPLAY=${DISPLAY} \"${xAPP}\" ${ARGS}"
###
###end###

I did quick tests:

# run-as chromium sakura
# run-as chromium seamonkey

This will be in the next release of EasyOS.    

Tags: easy