Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash run function with different user

Tags:

bash

sudo

Would be possible to run a custom bash function with different priviledges?

#!/bin/bash
function RunStefano() {
     while [ 1 ]; do
         echo "Ciao, ´/usr/bin/whoami´"
         sleep 10;
     done &
}
export -f RunStefano;
echo "Welcome, ´/usr/bin/whoami´"
sudo -u stefano -c "RunStefano"

If I run this script with 'root' user, I want to receive as output:

Welcome, root
Ciao, stefano
    (...)
Ciao, stefano

Would it be possibile?

like image 614
Stefano Radaelli Avatar asked Jul 29 '13 14:07

Stefano Radaelli


1 Answers

Yes, this is possible

#!/bin/bash
function1(){
   echo `whoami` 
}
export -f function1
su username -c "bash -c function1"
exit 0
like image 87
Richard Fletcher Avatar answered Oct 16 '22 01:10

Richard Fletcher