Ubuntu – logout current user from script

bashcommand linelogoutscripts

I would like to create a script that:

  1. run some commands
  2. execute sudo su – [some other username]
  3. after I exit from [some other username] -> logout

Example: test.sh

echo "test script"
sudo su

If I run a script like this:

echo "test"
sudo su

I get the wanted result:

[wolfy@ubuntusrv ~ ]# ./test.sh
[root@ubuntusrv wolfy]# date
Fri 19 Jun 10:56:52 CEST 2015
[root@ubuntusrv wolfy]# echo bla
[root@ubuntusrv wolfy]# exit
[wolfy@ubuntusrv ~ ]#

As you can see, the script runs my echo command and then I sudo su to root, where I can do what I need to do and after I quit root session the script execute the last (whoami) command.

My problem is that I can't logout myself at the end of this script.

I need a way to do this without root privileges.

Can someone help me?

Additional explanation:

  1. when I run sudo su – username (userA) I need to provide my password (wolfy) to login as the other user (userA). Then I work for the whole session with that user (userA) and when I logout from that user (userA) I would like to automatic logout from my (wofly) user.
  2. using sudo command is not an option

Best Answer

Perhaps all you want to do is:

exec ./test.sh

This replaces your current shell process by the script, so that when it finishes there is nothing left to run.

Related Question