tags:

views:

216

answers:

4

How can I log out of my computer using shell such the log-in window appears again?

I need this functionality in one my Linux script.

Update: I want to replicate the code working behind the Logout button of my Ubuntu.

Desktop Enviroment being used: GNOME

+1  A: 

Try restarting the display manager using one of the following, depending one which one you're using. You'll obviously have to have the proper permissions.

/etc/init.d/xdm restart
/etc/init.d/kdm restart
/etc/init.d/gdm restart
scompt.com
xdm kdm not available on my Ubuntu 10.0
baltusaj
That's probably not a good idea, as it will unceremoniously kill all X clients, possibly losing unsaved data in running apps :-(.
sleske
@baltusaj: Ubuntu 10.04 uses `gdm` by default
Piskvor
@baltusaj: That's because Ubuntu 10 uses `gdm` by default. Anyway, probably not a good solution (see my other comment).
sleske
@sleske: As far as I can tell, that's exactly what the OP wants. @baltusaj: I've added gdm, which is what Ubuntu 10.0 uses.
scompt.com
@scompt.com: Well, it does not *explicitly* say so. I find it very bad style to suggest solutions which can lead to data loss without at least mentioning that problem.
sleske
@scompt.com Great. That's what i needed. @sleske It's fine to me. If the user does this without saving his data before, that's his headache. :P Thanks for letting me know though.
baltusaj
+1  A: 

It's a bit of a hack, but the way that screen's power detach does this is to determine its parent pid and send it a SIGHUP. (Clarification: This closes only the shell, so only works if you're running from a console - not a graphical login - so may not be what you're looking for.)

crazyscot
+2  A: 

Your question is a bit vague. Are you trying to log out of a graphical session of a desktop environment (such as KDE/Gnome), as if the user clicked on "log off"?

Then you need to find out if/how the desktop environment supports scripting to log out. On KDE for example you can use kquitapp.

sleske
Yes exactly. I want to replicate the code working behind the Logout button in my ubuntu (GNOME).
baltusaj
Do you know what can I use for GNOME?
baltusaj
You could try gnome-session-save: http://linux.die.net/man/1/gnome-session-save
sleske
+5  A: 

I think this does the job:

gnome-session-save --logout

Also, take a look at the --help output, maybe you want to use the --force-logout option.

Usage:
  gnome-session-save [OPTION...]

Help Options:
  -h, --help               Show help options
  --help-all               Show all help options
  --help-gtk               Show GTK+ Options

Application Options:
  --logout                 Log out
  --force-logout           Log out, ignoring any existing inhibitors
  --logout-dialog          Show logout dialog
  --shutdown-dialog        Show shutdown dialog
  --gui                    Use dialog boxes for errors
  --display=DISPLAY        X display to use
Daniel M.
This page: http://linux.die.net/man/1/gnome-session-save says, it's --kill, not --logout, but I haven't tried.
Boldewyn
@Boldewyn: On my Gnome, a popup opens when I use --kill, maybe not what is wanted for scripts. I then ran gnome-session-save --help, which lists the --logout option.
Daniel M.
thanks. That did what I needed.
baltusaj
@Daniel M.: Just checked the man page on my Ubuntu machine, too: --kill and --silent are deprecated, so my comment from above is irrelevant, the linked man page is outdated. (I +1'ed your answer before anyway. Thanks for pointing us to gnome-session-save)
Boldewyn