1753821 Members
9190 Online
108805 Solutions
New Discussion юеВ

exiting a program

 
SOLVED
Go to solution
Dennis Kennedy
Advisor

exiting a program

Hello all,

I'm writing a shell script that when it exits to log off the user that started it(Posix shell if it matters in this case). There isn't a command to logoff or exit that I could call that I've found.
Thanks in advance,
just do it!
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: exiting a program

Hi Dennis:

If your script is one that you want to launch when the user logs in, and when it exits, the user is logged off, then their our two easy ways. These methods are well suited with menu-based scripts.

One method is to launch your script as the last command of the user's login profile, as:

# exec $HOME/my.script

The other method is to declare the script as the program to use in lieu of the normal shell in the sixth field of the user's /etc/passwd entry.

In either case, when the script exits, the user will be logged-off.

The advantage to the second method (making the default shell be your script) is that the user cannot escape into a shell from within your script. Remember that simple commands like 'more" have this ability, and you may want to restict that for security reasons.

Regards!

...JRF...
Andreas Voss
Honored Contributor

Re: exiting a program

Hi,

within your script you can kill the login shell with:

kill -9 `who am i -T|awk '{print $8}'`

Regards
linuxfan
Honored Contributor

Re: exiting a program

Hi Dennis,

I second Jim's option of defining the user's shell as the script, but if that is not an option, then what you can do is when the user logs in and runs the script, as the last thing do a kill on the PPID which would be the shell which spawns this script. If you are spawning lots of other child processes then i would recommend storing the PPID early on in the script to a variable and then do a kill on that variable as the last thing in the script.

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Dennis Kennedy
Advisor

Re: exiting a program

Thank you all for your help. I'm sure that both processes would work but I decided with the kill. I'm making a "User friendly" menu for some of our users and I need the environment settings in the .profile.

Thanks again!!
(I'll have a crown some day!! LOL)
just do it!
James R. Ferguson
Acclaimed Contributor

Re: exiting a program

Hi (again) Dennis:

One last point:

If you want your shell to have the environmental variables of its $HOME/.profile, then the 'exec' mechanism I first suggested would provide that. In addition, if your script wanted to offer the ability to "shell-out", it could. Having done so, the user could execute commands in the subshell and upon exiting the subshell, return to your script.

Regards!

...JRF...