Operating System - HP-UX
1835177 Members
2712 Online
110077 Solutions
New Discussion

Re: Trying to have a script log user off machine as last step.

 
SOLVED
Go to solution
Boyd Kodama
Frequent Advisor

Trying to have a script log user off machine as last step.

On a HP-UX 10.20 (series 700's), I am trying to write a script to log the user off the current machine as the last step.

What is the command to log off the machine or how can I do that in my script?

thanks,

Boyd

PS - I already know how to script a shutdown or reboot.
Without Mondays, there weren't be any Fridays
15 REPLIES 15
Ajay Sishodia
Frequent Advisor

Re: Trying to have a script log user off machine as last step.

try adding
exit
at the end of the script

later
Ajay
Magdi KAMAL
Respected Contributor

Re: Trying to have a script log user off machine as last step.

Hi Boyd,

You may define the user in the /etc/passwd like the following :

user1:*:uid:gid:Comment:homeDir:PathScript

PathScript: should be a script which direct the user to do what he is intended to do and one that user finish with that script "PathScript" the session terminates.

PathScript could be like : "/home/user1/StartAndFinish.sh".

Magdi
Sachin Patel
Honored Contributor
Solution

Re: Trying to have a script log user off machine as last step.

Hi Boyd,
I have two window when I run command who
#who
sachin ttyp1 Jul 27 09:48
sachin ttyp2 Jul 27 12:57
#tty
/dev/ttyp2
am in ttyp2

Now I can run ps -ef |grep ttyp2 from ttyp1 and kill -9 pids
On ttyp1
#ps -ef |grep ttyp2
sachin 2814 2813 0 12:57:36 ttyp2 0:00 csh
#kill -9 2814 2813


Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: Trying to have a script log user off machine as last step.

Hi:

Assuming that your user has logged in and runs the script in his login shell, then in the script as the last statement, do:

# exec /usr/bin/false

...JRF...
Bill Hassell
Honored Contributor

Re: Trying to have a script log user off machine as last step.

If you are running a script from a standard shell, then your script is run as a sub-process or child (fork is the Unix jargon) and when the script exits, it will return to the parent shell, not logout.

The easiest way to get an exit is to start the script with the dot command. The dot command is a single . followed by white space and then the script's name. If the script is called mybackup, then you would run the script with:

. /pathname/mybackup

See the dot? The current shell will execute each command and when exit is seen, it will exit which logs out.


Bill Hassell, sysadmin
Boyd Kodama
Frequent Advisor

Re: Trying to have a script log user off machine as last step.

Adding exit didn't log the user off the machine.
It just exits the script.

Boyd
Without Mondays, there weren't be any Fridays
Sachin Patel
Honored Contributor

Re: Trying to have a script log user off machine as last step.

Hi
My previous answer works for me. I have test it on my system. you just have to implement in your script.

Sachin
Is photography a hobby or another way to spend $
Boyd Kodama
Frequent Advisor

Re: Trying to have a script log user off machine as last step.

I tried adding the exec /usr/bin/false but that didn't
work.

I'm wondering if I may not be clearly stating what I
am trying to do. As the last step of a script, I want
the machine to be logged off just like it would
if I clicked on the EXIT icon on the CDE menu
at the bottom of my screen or if I clicked on my
Log Off menu option from the right click mouse
button menu.

This way, I can run my script that do a bunch of
stuff and leave for the day with the idea that the script
will log me off the machine as its last step. Thus,
the screen lock will not prohibit someone else that
may need to log on the machine, especially if I left
for the day.

thanks,

Boyd
Without Mondays, there weren't be any Fridays
Patrick Paton
Occasional Advisor

Re: Trying to have a script log user off machine as last step.

The last line of your script should be "exit 0" without the quotes. Then, as Bill has explained, launch your script with a . /path_to_script. Make sure to put a space after the dot, not ./ as in search the current directory. This will force the system to execute the script in the current shell rather than spawning a new shell. The "exit 0" will logoff the current shell as the script exits.
Shannon Petry
Honored Contributor

Re: Trying to have a script log user off machine as last step.

Boy Sachin, you do it the hard way. "who -u" will give you the PID associated with any TTY, and it's much easier to process and kill!

As for the user, Bill Hassel gave the best answer, but you have to think about how your script is bein accesses. If people are sitting at a terminal, and opening a dtterm from CDE, It will Not work! If they are using telnet to access the machine, then it will. It would also work if you put your script as the log-in shell for the user. Remember that these it is easy to get around this type of, for lack of better terms, security.

If I telnet to your box, and am given a shell, I can type "^Z", "^C", "^Q", or if I am given a prompt at any time, could issue a few shells of my own from the prompt to circumvent what your trying to do.

If all they need is an shell with some prompts you give them, then you can trap any interupt sent by the user.

Regards,
Shannon
Microsoft. When do you want a virus today?
Sachin Patel
Honored Contributor

Re: Trying to have a script log user off machine as last step.

Hi Shannon,
I agree. But the question wasn't clear.

Sachin
Is photography a hobby or another way to spend $
Chris Calabrese
Valued Contributor

Re: Trying to have a script log user off machine as last step.

One way to do this is to kill the window manager by finding its PID using PS as described earlier.

Another solution is to change CDE to execute your script on logoff.

Upon logoff, CDE executes whatever the resource Dtlogin*reset: points to, which is set to 'Xreset' by default (I'm looking at the various stuff in /usr/dt/config here). You can redefine this to call your script, which should call the real Xreset when it's done.
Brainbench MVP for Unix Administration and Internet Security, SANS Review Editor, and Center for Internet Security HP-UX Benchmark project leader
Joseph C. Denman
Honored Contributor

Re: Trying to have a script log user off machine as last step.

I think this is what you are looking for???

Do This:

nohup /my/script/to/run.sh &

Then just exit. The script will continue to run even after you log off.

...jcd...
If I had only read the instructions first??
Rolf Lundqvist
New Member

Re: Trying to have a script log user off machine as last step.

Hi,
If I understand it correctly you want to start non-interactive scripts and go home, then have the terminal logged out!?
One way of doing this is to use the "batch" command.
Put your scripts in a file, then:
$ batch < job-file
job 996497765.b at Mon Jul 30 14:56:05 2001
$ logout

This way the terminal will be available directly, job secured and detached from terminal.
Output from the job will be mailed to you.
(see "man batch")

Good Luck!
/Rolf
Boyd Kodama
Frequent Advisor

Re: Trying to have a script log user off machine as last step.

Well, thanks for all your input. It seems Sachin with Shannon's comment and Chris had the best approaches for my script.

I ended up with the following two possibilitities.

who -u | grep console | kill -9 `awk '{ print $7 }'`

or

ps -ef | grep dtsession | grep -v grep | kill -9 `awk '{ print $3 }'`

The first one is dependent on the idea that the 7th field will always be the PID.

The second one came from Chris' idea of terminating the window manager process (dtwm). But, I found that I need to terminate the parent process of the dtsession process which is the parent process of the dtwm process.

The PID of the first approach is the same PID of the second approach.

thanks,

Boyd
Without Mondays, there weren't be any Fridays