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.
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'
Try this command :
First off let's clear some basics before I begin my answer
- One terminal can only have one login shell, however multiple users can be using the same terminal with non-login shell.
- This login shell can be a TTY or PTS (Difference between tty and pts)
- The login shell can have other non login shells (What are login/non-login shells)
exitis for exiting from non-login shell (You cannot
logoutfrom non-login shell)
logoutis for logging out of the TTY (
logoutcan only be used with login-shell)
- 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;
Remove "<" and ">" tags and place your task in the above scripts.
I don't know why
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.