首页 > 解决方案 > Runuser 命令在 ubuntu 启动时不起作用

问题描述

我创建了一个启动脚本,它使用runuser命令来获取usersitepackagespython3。这在 时可以正常工作sudo /etc/init.d/myservice start,但在设备启动时不行。

USER_NAME=$(if [ $(whoami) = "root" ]; then echo $SUDO_USER; else echo $USER; fi)
if [ $(whoami) = "root" ]; then
  PKG_ROOT=$(runuser -l $USER_NAME -c "python3 -c 'import site; print(site.getusersitepackages())'")
  CELERY_BIN=$(runuser -l $USER_NAME -c "which celery")
  USER_HOME="/home/$USER_NAME"
else
  PKG_ROOT=$(python3 -c 'import site; print(site.getusersitepackages())')
  CELERY_BIN=$(which celery)
  USER_HOME=$(echo $HOME)
fi

我想要 $PKG_ROOT 之类的/home/<user_name>/.local/lib/python3.7/site-packages,但我总是得到/root/.local/lib/python3.7/site-packages. 也不能使用 $USER_NAME。

标签: shellubuntu-18.04

解决方案


推荐阅读