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.

  • 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
    Commented Jan 24, 2016 at 9:11
  • I want to logout from terminal of current user
    – anurag
    Commented Jan 24, 2016 at 12:32

4 Answers 4


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.

alias exit='./your_script && exit'
  • I am trying to logout via shell script...but this above statement also not working.....and not printing anything for my else statement
    – anurag
    Commented Jan 24, 2016 at 8:17
  • @anurag - i dont see any else statement
    – mikeserv
    Commented Jan 24, 2016 at 8: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
    Commented Jan 24, 2016 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
    Commented Jan 24, 2016 at 15:47

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


  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


(./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.


Try this command :

killall gnome-session

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .