The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
2

Run a command when i3 exits?

asked 2014-03-03 22:44:51 +0000

annoyed gravatar image

updated 2014-03-15 05:36:28 +0000

Hi, I know you can use "exec" in your i3 config file to run something when you log in, but is there any way to have it run something when you sign out?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
4

answered 2014-03-14 02:55:56 +0000

TonyC gravatar image

You can do this with the new i3ipc library (Python, Lua, JavaScript)

#!/usr/bin/env python3

from gi.repository import i3ipc

def on_shutdown(conn):
    print('goodbye, world')

conn = i3ipc.Connection()

conn.on('ipc_shutdown', on_shutdown)

conn.main()

The event will fire whenever the connection to the ipc is lost (including on i3-msg restart) and the main loop will end afterwards.

Now put this line in your config:

exec_always --no-startup-id ~/bin/i3-shutdown.py
edit flag offensive delete link more

Comments

I would LOVE this solution if i3ipc was in my distribution's repositories. I'm using version 4.6 of i3 on openSUSE.

annoyed gravatar imageannoyed ( 2014-03-15 05:33:11 +0000 )edit

what about a pip or luarocks package?

TonyC gravatar imageTonyC ( 2014-03-27 04:26:29 +0000 )edit
1

answered 2014-03-05 19:01:04 +0000

KJ44 gravatar image

updated 2014-03-05 19:06:05 +0000

This should give you the general idea.

bindsym $mod+e mode "Exit"

# Exit i3 in various ways.                                                                                                                                           

mode "Exit" {
    bindsym h exec gksu "/sbin/shutdown -h now"
    bindsym r exec gksu "/sbin/shutdown -r now"
    bindsym x exit

    # back to normal: Enter or Escape                                                                                                                                
    bindsym Return mode "default"
    bindsym Escape mode "default"
}
edit flag offensive delete link more

Comments

You can insert scripts to do what you want based on the above. Bear in mind that if you try to chain 'exit' after your script, it might not have time enough to do its job before i3 exits.

KJ44 gravatar imageKJ44 ( 2014-03-05 19:04:43 +0000 )edit

Is there anyway I can make i3 enter this mode automatically? Like, if I don't want to have to press a key combination (for cases where I'm not signaling i3 to close normally)?

annoyed gravatar imageannoyed ( 2014-03-09 08:13:21 +0000 )edit

Maybe i3-msg? Sorry, but you aren't explaining what you want in enough detail. What causes i3 to be signalled in your use case.

KJ44 gravatar imageKJ44 ( 2014-03-10 21:22:48 +0000 )edit

I generally just straight up shutdown my computer rather than log off, so "systemd poweroff".

annoyed gravatar imageannoyed ( 2014-03-15 05:28:03 +0000 )edit

Question Tools

Stats

Asked: 2014-03-03 22:44:51 +0000

Seen: 413 times

Last updated: Mar 15 '14