2

I am doing some operation via shell script. If that script is successful, continue else logout of the current user. I am unable to find a way to logout of current user. I tried to use exit but seems like it is exiting from the current process, not the shell. PS: I cannot use pkill to kill session for current user as there are many process which runs through that user as it is a generic user. Also since this is a generic user, multiple other team mates may be logged in to the system via that user and it may kill their session as well.

anurag
  • 362
  • 1
    You're saying you want to logout, but logout from what exactly? Do you only have an open terminal session or do you have any Desktop Environment running as well? – replay Jan 24 '16 at 09:11
  • I want to logout from terminal of current user – anurag Jan 24 '16 at 12:32

4 Answers4

2

If the script is for the same shell as the one you are using when you run it, you could just source it instead of running it as a new process, then exit should log you out successfully.

i.e. instead of ./script.sh you do source ./script.sh. The reason for this difference is that source is a shell builtin command which makes your current shell execute the commands in the file, if you run the script without source a new process is created to execute it and that's why exiting it doesn't exit your shell.

replay
  • 8,553
  • 1
  • 27
  • 31
0
alias exit='./your_script && exit'
mikeserv
  • 58,310
  • I am trying to logout via shell script...but this above statement also not working.....and not printing anything for my else statement – anurag Jan 24 '16 at 08:17
  • @anurag - i dont see any else statement – mikeserv Jan 24 '16 at 08:27
  • I am printing some messages and doing some calculations with ak user. If those calculations are matching with my results keep continue doing that task and print the messages on terminal else logout that user from terminal – anurag Jan 24 '16 at 12:34
  • @anurag - Youre going to need to show some demonstrative code - because i dont follow, man. Im sorry. The above command will alter the foreground shell's interpretation of a foreground shell's exit command. Rather than directly exiting it will call your_script first and only continue to exit if the return of your script is 0. Also, it only defines the new form of exit, after doing the above and entering a newline into a terminal prompt if you call exit at some point the shell will replace that call with the quoted code above when it reads it. Thats all - its just an input macro. – mikeserv Jan 24 '16 at 15:47
0

First off let's clear some basics before I begin my answer

AFAIK

  1. One terminal can only have one login shell, however multiple users can be using the same terminal with non-login shell.
  2. This login shell can be a TTY or PTS (Difference between tty and pts)
  3. The login shell can have other non login shells (What are login/non-login shells)
  4. exit is for exiting from non-login shell (You cannot logout from non-login shell)
  5. logout is for logging out of the TTY (logout can only be used with login-shell)
  6. How to check if you are on a login or non-login terminal

If you are on TTY

(./your_script && <task_you want_to_continue>) || logout

or

(./your_script && <task_you want_to_continue>) || exit

If you are on PTS

(./your_script && <task_you want_to_continue>) || who -u; 
echo 'enter PID of login shell user you want to logout'; read PID; kill $PID;

Source of above answer

Remove "<" and ">" tags and place your task in the above scripts.

I don't know why logout and exit do not work on PTS as they do on TTY, but I hope this solves your problem.

I have been about 4 months on Linux, so by that standard I should be called a newbie. Any improvements to the answer are appreciated if you find anything wrong.

-1

Try this command :

killall gnome-session
Tiger
  • 666