LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Logging out a user account within a script (https://www.linuxquestions.org/questions/linux-software-2/logging-out-a-user-account-within-a-script-711461/)

kaplan71 03-13-2009 05:38 PM

Logging out a user account within a script
 
Hi there --

I am writing a shell script that will do a snapshot of a local hard drive, but there is a need for all users to be logged off when the script is run. This is due to the user accounts being NIS, and the home directories in question are NFS mounts which are supposed to be unmounted prior to the snapshot being made.

What would the correct syntax be in order to accomplish this? One idea that I had was the following:

Code:

# su - <user>
# logout

If this works, how could I go about in checking for all logged in users, and repeating the above process? Thanks.

rjlee 03-13-2009 06:53 PM

Quote:

Originally Posted by kaplan71 (Post 3474808)
What would the correct syntax be in order to accomplish this? One idea that I had was the following:

Code:

# su - <user>
# logout

If this works, how could I go about in checking for all logged in users, and repeating the above process? Thanks.


That won't work. At best, it will just create a new login shell for the given user and just log out of that shell.

If you have text-mode users, I would first ask the users to log out, then wait for a length of time:

Code:

echo "Please log out to unmount all NIS accounts" | wall
sleep 60

You then need to kill any login shells that persist:

Code:

who -lH
The column labelled PID contains the process IDs of any login shells, which can be used to kill the shell process. Rather than killing the script outright, you want to tell these shells to log out so that their logout scripts run:

Code:

kill -HUP pid

chrism01 03-14-2009 12:07 AM

To check for logged in users/open files, look at
fuser
lsof


All times are GMT -5. The time now is 01:50 AM.