Bash在脚本中退出sudo [英] Bash dropping out of sudo in a script

查看:181
本文介绍了Bash在脚本中退出sudo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用sudo执行安装脚本,但是在脚本结束时,该脚本需要退出sudo并以普通用户身份继续.

I need to execute an install script using sudo, but towards the end of the script, the script needs to drop out of sudo and continue as the regular user.

示例:

sudo ./install.sh

脚本运行并以root用户身份执行所需的操作

script runs and does what it needs to as root

su myscriptuser
service myscript start

基本上,服务myscript start必须由普通用户而不是root用户运行.

Basically, the service myscript start needs to be run by the regular user, not by root.

推荐答案

su myscriptusermyscriptuser的名称启动另一个shell,并等待直到退出.然后继续以root的名义运行service myscript start.

su myscriptuser starts another shell in the name of myscriptuser and waits until it exits. Then it proceeds to run service myscript start in the name of root again.

您需要的而不是最后两个命令是sudo:

What you need instead of the last 2 commands is sudo:

sudo -u myscriptuser service myscript start

这篇关于Bash在脚本中退出sudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆