LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-02-2007, 01:35 PM   #1
fmcauley
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Rep: Reputation: 0
Shell script trap and log out a user


Hi,
I have a shell script that i am working on. I need to have the user log out if they try to break the control (CTRL + C). I have used the trap command but this only seems to exit the program and the user would then have access to the shell. I would like to have the user logged out with out accss to the shell. :

#clean up the tempfile
trap cleanup 0 1 2 3 6 9 14 15

cleanup()
{
echo "Caught Signal ... cleaning up."
rm -rf /tmp/menuitem.*
echo "Done cleanup ... quitting."
exit 1
}

Would i have to write a kill() function? of some sort. I have tried the skill with the -KILL -u username but that seems to lock up the PC.

Thanks
Frank
 
Old 03-02-2007, 03:56 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
If the ctrl-c is doing what you want (exiting the program) then you just need to exec the program at the beginning to insure they don't go to a shell.

Normally when a person logs in they are running a shell (e.g. bash). Any commands that are typed thereafter are "forks" (children) of the shell. If however you start the command with "exec" it is not a "forked" child but rather replaces the original shell. Because of this when the "execed" process is exited there is no parent shell to fall back on so it will simply exit completely as it would if they had typed "exit" at the shell prompt.

You can see the difference in the way things look by running a separate terminal then doing a ps -ft<tty> of the terminal where the user is logged in (you can determine tty by typing "tty" in the user's terminal).

Try it with and without the exec and you'll see in the one the bash still shows up but in the one with exec it goes away.

Once upon a time I was trying to autolaunch apps for users and did exactly this. I would exec the app from their .profile. Also in the /etc/profile I did something like:
trap "" 1 2 3
That turned off all traps so the user couldn't ctrl-c out before he got to the exec in the .profile. (Some users figure out that by hitting ctrl-c multiple times quickly on a login that they can do that if you haven't turned off the traps.)

By the way you can always change the key used for "kill" from ctrl-c to something else by using the stty command.
 
Old 03-02-2007, 04:35 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
One problem with your approach. When your script calls exit, it can only exit the shell that is running the script. If the script is runn as a child process of another, parent, shell then the parent shell will resume control.
I think your best bet is to have the parent process look at the exit value of your script, and exit if it is non-zero, which it will be if the script is killed. Another (untested) alternative:
Code:
cleanup()
{
echo "Caught Signal ... cleaning up."
rm -rf /tmp/menuitem.*
echo "Done cleanup ... quitting."
kill -9 $PPID
}
--- rod.
 
Old 03-02-2007, 04:47 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
I'm assuming that was a response to the original question rather than to what I wrote. The exec I spoke of is specifically to prevent it being a sub-process of a shell but rather a replacement for the shell on invocation.
 
Old 03-02-2007, 05:05 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by jlightner
I'm assuming that was a response to the original question rather than to what I wrote.
Yup. I was still writing while you were posting. Good point about 'exec'.
--- rod.
 
Old 03-05-2007, 01:48 PM   #6
fmcauley
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Original Poster
Rep: Reputation: 0
thanks

Thanks for the tips. I am trying to build a set of tools for an end user group and i want to keep them from access the shell. I only want them to run the script(s) and then logout.

Frank
 
Old 03-06-2007, 11:10 PM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by fmcauley
Hi,
I have a shell script that i am working on. I need to have the user log out if they try to break the control (CTRL + C). I have used the trap command but this only seems to exit the program and the user would then have access to the shell. I would like to have the user logged out with out accss to the shell. :
Make your script the user's login shell,
 
  


Reply

Tags
logout, script, shell, trap


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script For Myspace Log in condonm Programming 1 02-08-2007 07:34 AM
Create a shell Script that log in as root mashed Linux - Newbie 1 04-08-2006 05:13 AM
user log in and autoruns script rharris72 Programming 1 11-21-2005 10:14 AM
shell script user not ok slam Linux - General 4 07-24-2003 07:40 AM
shell script - trap program termination dnijaguar Linux - Software 0 07-19-2003 01:27 AM


All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration