Auto logout from a virtual terminal after execu... [SOLVED]

Forum rules
Before you post please read this

Auto logout from a virtual terminal after execu... [SOLVED]

Postby Pilosopong Tasyo on Fri Dec 16, 2011 6:45 am

Is it possible to automatically log out a user from a virtual terminal after running a shell script? For example,

test.sh
Code: Select all
#!/bin/sh
ls -l
logout

The script will list the contents of the user's home directory but it won't logout. Instead, it gives a logout: not found error message. But manually typing 'logout' (or 'exit' for that matter) and pressing the enter key at the prompt works. :?:
Last edited by Pilosopong Tasyo on Sat Dec 17, 2011 1:45 am, edited 1 time in total.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Linux Mint is funded by ads and donations.
 

Re: Auto logout from a virtual terminal after executing a sc

Postby Habitual on Fri Dec 16, 2011 12:04 pm

how about exit in the script?
or
Code: Select all
TMOUT=xxx


TMOUT=1 in a terminal quits the terminal emulator.
it should work?
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1998
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

Re: Auto logout from a virtual terminal after executing a sc

Postby Pilosopong Tasyo on Fri Dec 16, 2011 10:30 pm

I tried replacing logout with exit before, but that action only ends execution of the test script. It does not log the user out the virtual console. Setting the TMOUT variable within the script doesn't do it either. Manually setting it at the prompt, however, does log the user out. What I'm trying to do is run a script as root under tty1 (or any of the other virtual consoles). Its purpose is to manually set the countdown timer on the terminal where it's stored. It's my backup mechanism (and protocol) in case the server that normally does this, is not available or goes down for maintenance:

new-time.sh
Code: Select all
#!/bin/sh

##########
#
# Project     : Cybercafé Timer Project (CTP)
# Started     : December 12, 2011
# Last Edited : December 12, 2011
# Module      : new-time.sh
# Description : Sets the countdown timer.  Syntax is:
#               ./new-time.sh <minutes>
#
#               This script should only be used in case the server
#               is not available.  Log in as root in any virtual
#               terminal (preferably tty1), change directory to
#               /ctp/client/control-panel and execute the script
#               together with any required parameter(s).
#
##########

. /ctp/client/support/config.sh

MINUTO=$1
if [ -z "$MINUTO" ]
then
  echo "Usage: $0 <minutes>"
else
  PLACEHOLDER=`tempfile`
  echo "`whoami`@`hostname`" >> $PLACEHOLDER
  echo "countdown"           >> $PLACEHOLDER
  echo "$MINUTO"             >> $PLACEHOLDER
  mv $PLACEHOLDER $COMMAND_MESSAGE_FILE
  clear
  echo "Type 'logout' and press the enter key."
fi

# EOF


So, I'm looking for something that replaces this line:

Code: Select all
echo "Type 'logout' and press the enter key."

so that root gets automatically logged out when the script finishes it's work.

Any other insights? Thanks.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Auto logout from a virtual terminal after executing a sc

Postby Habitual on Fri Dec 16, 2011 11:14 pm

I'm intrigued...

1.) you are stumped.
2.) How to make it work.

I'll think about this tomorrow and re-visit what can be done,
or ask a bunch of stupid Questions or toss out some silly answer you, no doubt have already tried.
But what are friends for? :wink:
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1998
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

Re: Auto logout from a virtual terminal after executing a sc

Postby Pilosopong Tasyo on Sat Dec 17, 2011 1:42 am

Nevermind. Found a way while experimenting earlier.

Putting

Code: Select all
pkill -SIGHUP `basename $SHELL`
or
Code: Select all
pkill -SIGKILL `basename $SHELL`

at key places ought to do it. Thanks for trying though. Marking this thread solved.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby Habitual on Sat Dec 17, 2011 12:12 pm

I knew you would!

I'd be interested in the snippet that does that task. :)
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1998
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby Habitual on Wed Dec 28, 2011 4:24 pm

Just found this gem....

Code: Select all
who mom loves


worked on Ubuntu 10.10, CentOS 5.5, and OpenSUSE 11.4

Scary, isn't it?
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1998
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

Re: Auto logout from a virtual terminal after executing a sc

Postby mph426 on Tue Feb 07, 2012 10:47 pm

Pilosopong Tasyo wrote:Nevermind. Found a way while experimenting earlier.

Putting

Code: Select all
pkill -SIGHUP `basename $SHELL`
or
Code: Select all
pkill -SIGKILL `basename $SHELL`

at key places ought to do it. Thanks for trying though. Marking this thread solved.


I don't mean to beat a dead horse here but when pkill command as shown, it closed all terminals using that $SHELL.

Here's how you can kill just the terminal your working in.
Code: Select all
kill -SIGKILL $$
or
Code: Select all
 kill -9 $$
I'd rather have a bottle in front of me than a frontal lobotomy.
mph426
Level 1
Level 1
 
Posts: 23
Joined: Fri Dec 16, 2011 3:03 pm
Location: State of confusion

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby Pilosopong Tasyo on Tue Feb 07, 2012 11:48 pm

mph426 wrote:I don't mean to beat a dead horse here but...

Actually, you didn't. I just learned a better way to implement what I'm doing with the snippet of code you posted. It didn't occur to me that using pkill...$SHELL will close other instances of it across other logged in users. Well, considering that my users don't have a use for a terminal session, so it never crossed my mind.

Looks like some modifications to my scripts are in order. Thank you! :D
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby Pilosopong Tasyo on Fri Feb 10, 2012 5:16 am

Ok, it looks like I spoke (well, wrote technically) too soon! :lol: Invoking:

Code: Select all
kill -SIGKILL $$

inside a script only terminates execution of that script. It will not log out the user from the virtual console. From what I can surmise, in order to end a login session from within a virtual terminal, the process that was invoked upon login has to be the one terminated (the parent shell). By running a script (and thereby creating another instance of the shell), the PID returned by the $$ variable inside that script is not the same one as that of the parent shell, which I'm trying to terminate in the first place. A quick (and dirty) way to achieve this is the method I originally posted earlier. It does have the unwanted side-effect, although I can live with that. As I said previously, my users don't have a need to invoke a terminal session, so, the unwanted side-effect is pretty much a non-issue.

Using $$ in lieu of `basename $SHELL` will work if I source the script at the shell prompt, e.g.:

Code: Select all
$ . new-time.sh 60

instead of invoking it as a separate process, like so:

Code: Select all
$ ./new-time.sh 60
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby mph426 on Tue Feb 14, 2012 11:57 pm

Sorry, I didn't see your response till now. I forget to check the notify me box.

Are you using a virtual console or a virtual terminal?

Here's another thing you might want to try is:
Code: Select all
pkill -u "$USER"
That will kill all processes owned by a user. If they are logged in, locally it'll kill everything they own. If they're logged in via ssh to a remote system, it will log them out. However, it won't close the window that they were logged in to. If they're logged in locally to the console, it will log them out.

Remember, if they ARE logged in via telnet, ssh, etc... and they have other processes running... they won't. :shock:
I'd rather have a bottle in front of me than a frontal lobotomy.
mph426
Level 1
Level 1
 
Posts: 23
Joined: Fri Dec 16, 2011 3:03 pm
Location: State of confusion

Re: Auto logout from a virtual terminal after execu... [SOLV

Postby Pilosopong Tasyo on Wed Feb 15, 2012 1:27 am

Yes, I locally log in as root via virtual terminal (tty1). Killing the root user is a complete no go since that essentially 'crashes' the sytem :lol:

Don't worry about it. I'm satisfied using the method I originally used. :D Besides, the scripts I wrote (apart from the new-time.sh one) are for internal use only (I'm the only one who has access to them).

Thanks!
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests