Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want, when a linux user exits from a shell script, that it also logs out from the bash shell. Is this possible?

share|improve this question
add comment

5 Answers

Instead of starting the script normally, exec it instead. This will replace the login shell, so when the script finishes the user will be logged out.

share|improve this answer
add comment

Depending on what you are trying to accomplish, you could use the script name instead of the shell in /etc/passwd.

The last entry in /etc/passwd (everything after the last colon) is the shell that is run when the user logs on. By changing this to the name of your script, when the script ends, then by definition, so does the shell.

** Be very careful editing /etc/passwd however, as you could lock yourself out of your machine. Apparently you can do this with

usermod --shell <script name> <user name>

which would be the safer way to make this change.

share|improve this answer
add comment

End your script with kill -HUP $PPID

$PPID is the process ID of the parent process.

share|improve this answer
    
$PPID might fail if the script is run from a subshell, e.g. bash login shell → another bash shell → this script. Also why -9 and not -HUP? –  Cristian Ciupitu Oct 6 '10 at 13:57
    
kill -9 closes all processes it can without letting them close their file descriptors, freeing their memory, etc... I don't think it's a good idea if the script does any I/O. –  Julien Vehent Oct 6 '10 at 16:04
    
Yes, you are right. Feeling a little bit destructive today ;) –  SvW Oct 6 '10 at 16:27
add comment

with kill ?

$ kill -HUP `ps -ef |grep $USER|grep bash|awk {'print $2'}`

Edit: as said in the comments, it works better with PPID

$ kill -HUP $PPID
share|improve this answer
1  
There is a variable for that: $PPID –  SvW Oct 6 '10 at 13:39
    
Yes, definitely use $PPID. Using ps in that way will log the user out of other sessions, too, which may be very undesirable. –  Dennis Williamson Oct 6 '10 at 14:12
add comment

It is possible to do this if you include an EXIT at the end of your script, which tells the bash shell to terminate.

share|improve this answer
1  
the exit will exit the current bash script, and not the bash of the user running it –  Julien Vehent Oct 6 '10 at 13:30
    
no exit will terminate only that script not the bash shell –  user56285 Oct 6 '10 at 13:37
    
This will just exit the sub-shell where the script is executed and doesn't solve the question. –  SvW Oct 6 '10 at 13:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.