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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
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].
Habitual

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

Post by Habitual »

how about exit in the script?
or

Code: Select all

TMOUT=xxx
TMOUT=1 in a terminal quits the terminal emulator.
it should work?
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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.
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].
Habitual

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

Post by Habitual »

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:
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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.
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].
Habitual

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

Post by Habitual »

I knew you would!

I'd be interested in the snippet that does that task. :)
Habitual

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

Post by Habitual »

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?
mph426

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

Post by mph426 »

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 $$
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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
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: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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
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].
mph426

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

Post by mph426 »

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:
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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!
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].
Locked

Return to “Scripts & Bash”