Startup script for i3 with Conditional Execution

I’ve been using the i3 window manager for roughly a couple of years now and have enjoyed playing with all the ways I’ve found to extend it through startup scripts, keybindings, etc. This little hack for enabling or disabling the launch of a group of applications at login is one of my favorites and I thought I’d share.

I’ve got a script in my i3 configuration directory called that fires up a number of persistent applications and launches things I normally use.

The first thing it does is load a saved layout and launch terminals with htop, dmesg –follow, and tail -f /var/log/syslog . I use this as a general system monitor workspace, a dedicated place I can go to see what’s happening on my system.

Next, it switches to a workspace I use as a dedicated terminal and launches urxvt, then switches to the workspace I use for my browser, switches the layout to tabbed, then launches my browser.

Then it loads up the scratchpad text app I use (Fromscratch), udiskie for managing removable media, the mpd daemon, and after some delay to make sure everything else is finished loading, dropbox.

I’ve been happy with this arrangement, but really wanted to find a way to keep it from launching some of those items on startup in case I wanted to do something more system intensive and didn’t need those things right away or want to do some kind of benchmarking and want fewer things tying up RAM.

Here’s the solution I found:

#load saved system monitor workspace and populate
i3-msg 'workspace "S"; append_layout ~/.config/i3/monitor3.json;'
sleep 0.25
urxvt -e "htop" &
sleep 1
urxvt -e sh -c "dmesg --follow" &
sleep 0.5
urxvt -e sh -c "tail -f /var/log/syslog" &
sleep 1

#read the current capslock state - result should be "on" or "off"
caps=`xset -q | grep Caps | awk '{ print $4 }'`

#if capslock is on at login, skip running these apps at startup
if [ $caps = off ]; then

        i3-msg 'workspace "3:>_"'
        urxvt &
        sleep 1
        i3-msg 'workspace "1:B"; layout tabbed;'
        sleep 0.5 
        google-chrome &
        sleep 10
        fromscratch &
        udiskie -s -f "/home/alex/bin/rangerwrapper" &
        mpd &
        autocutsel &
        sleep 30 #wait a while to launch dropbox so everything else can finish starting since it's a resource hog
        dropbox start &
        #Caps lock was enabled, so skip all the above after the monitor workspace and drop to the first workspace
        i3-msg 'workspace "1:B"'
        #since we got here because caps lock was on at login, simulate that keypress to toggle it off.
        xdotool key Caps_Lock

The idea here is that I can log in normally and get all my normal startup apps, or if I know I want a trimmed-down session, I can type my password to log in, then hit caps lock before hitting enter to log in. Beyond that, hopefully the comments make its function fairly clear.

It’s worth noting that I tried to do this with num lock, but discovered that while numlock is off at my login screen (lightdm with the gtk greeter), it is automatically turned on at some point as i3 starts, and before my script runs. Without figuring out how to disable that (or where it’s coming from) I can’t really use that key this way.

I also tried with scroll lock, but discovered that my scroll lock key isn’t actually tied to the scroll lock function. While I was able to fix that behavior at runtime, I couldn’t make it happen before I had a user session (tried messing with lightdm init scripts, with xinit scripts, etc, and couldn’t get it), which meant I had no way to turn on scroll lock until after I was logged in which defeated the purpose. In case it’s helpful, here’s the command that fixed scroll lock for me:
xmodmap -e "add mod3 = Scroll_Lock"

I’ve got other misc startup commands in my i3config, but those are all just adjusting settings at runtime, setting my wallpaper, etc., and indeed it’s the i3config that executes this launch script.

Leave a Reply