[CentOS-devel] Environment variable $USER not getting updated on using 'exec sudo command'

Trevor Hemsley

trevor.hemsley at ntlworld.com
Wed Apr 11 08:06:42 UTC 2018


On 11/04/18 07:31, Veetil, Vyshnav wrote:
>
> Hi All, 
> We are facing an issue with exec sudo command which is resulting in an
> infinite loop. 
> We have one script in which we are checking if $USER is not equal to
> root then execute the same script with exec sudo so that it will run
> with root. 
> Code snippet: 
> script1: 
>
> if [[ "$USER" != "root" ]]; then 
>     exec sudo $0 $@ 
> fi 
> If we run the script with some another user say root1 using below 
> "sudo -u root1 script1" script1 will run in infinite loop. 
> In CentOS 7.4 when exec sudo command runs , the value of $USER remains
> same i.e root1 and again it enters and if block and execute the scipt1
> and so on .. 
> While in case of Centos 7.3 after running exec sudo command value of
> $USER gets updated to root and it works fine
>

Don't check $USER, use the output from `id -u`

Trevor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.centos.org/pipermail/centos-devel/attachments/20180411/69c864d5/attachment-0002.html>


More information about the CentOS-devel mailing list