ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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:
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.
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.
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. :
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.