You are not logged in.

#1 2012-08-06 11:48:27

C5OK5Y
Member
Registered: 2010-07-04
Posts: 48

[SOLVED] Problem autostarting applications with lua in Awesome

This is what I made (it's based on last example from here, right before the Directory way):

local function run_once(prg, arg_string, xname, pname)
    if not prg then
        do return nil end
    end

    if not pname then
       pname = prg
    end

    if not arg_string and not xname then
        awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")")
    elseif arg_string and xname then
        awful.util.spawn_with_shell("pgrep -f -u $USER '\\" .. arg_string .. "' || (" .. prg .. " " .. xname .. " " .. arg_string .. ")")
    elseif arg_string and not xname then
        awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. " " .. arg_string .. "' || (" .. prg .. " " .. arg_string .. ")")
    else
        awful.util.spawn_with_shell("pgrep -f -u $USER '" .. pname .. "' || (" .. prg .. " " .. xname .. ")")
    end
end

run_once("mpd")
run_once("devmon","--internal --ignore-device /dev/sda5 --ignore-device /dev/sda6 --ignore-device /dev/sda1 --ignore-device /dev/sda7 --ignore-device /dev/sda8",nil,"/bin/bash /usr/bin/devmon")
run_once("urxvt","-e newsbeuter","-name newsbeuter-autorun")
run_once("urxvt","-e mutt","-name mutt-autorun")
run_once("urxvt","-e irssi","-name irssi-autorun")
run_once("urxvt","-e mcabber","-name mcabber-autorun")
run_once("urxvt","-e ncmpcpp","-name ncmpcpp-autorun")
run_once("transmission-gtk",nil,"--name transmission-gtk-autorun")
run_once("chromium",nil,"--name=chromium-autorun","/usr/lib/chromium/chromium")

I'll explain the code a bit - prg contains the program to execute, arg_string contains the parameters the program will be launched with, xname contains a parameter that will set the X window name to a custom value for further usage and pname contains the process name if it's different from prg. xname is separate from arg_string because when I pgrep for the program, I want to check if it wasn't run without the xname previously (i.e. manually launched before the autostarting takes place). Then there are a lot of ifs to be able to achieve the most accurate pgrep in relation to the arguments that are passed to the program an finally execute the program with correct parameters. The parts if not arg_string and not xname then ... and elseif arg_string and not xname then ... (i.e. launching mpd and devmon) work correctly but the other two (elseif arg_string and xname then ... and else ...) don't work - for some reason they don't run the program when they should. I even tried to type the resulting pgrep (or rather what should be the resulting pgrep) into the terminal - pgrep -f -u $USER '\\-e ncmpcpp' || (urxvt -name ncmpcpp-autorun -e ncmpcpp) and it works correctly. So there's a problem it the lua code that I'm not aware of. Does anyone know where the problem is? Thanks.

Edit: How I solved by rewriting the autorun script into bash:

#!/bin/sh

run_once () {

  # move parameters to separate variables for 'further manipulation'
  prg=$1
  if [ -n "$2" ]; then
    arg_string=$2
  fi
  if [ -n "$3" ]; then
    xname=$3
  fi
  if [ -n "$4" ]; then
    pname=$4
  else
    pname="$prg" # this is the 'further manipulation'
  fi

  # output goes to /dev/null because if this function gets called from ~/.profile by bash on login, unneeded output can end in the shell
  if [ -z "$arg_string" ] && [ -z "$xname" ]; then                       # no arg_string, no xname
    pgrep -f -u $USER -x "$pname" > /dev/null || daemon -X $prg -n $prg
  elif [ -n "$arg_string" ] && [ -n "$xname" ]; then                     # has arg_string, has xname
    pgrep -f -u $USER '\'"$arg_string" > /dev/null || daemon -X "$prg $xname $arg_string" -n $prg$(echo "$arg_string" | sed "s/ /-/") # sed gets rid of spaces in parameters because daemon doesn't accept process names with spaces
  elif [ -n "$arg_string" ] && [ -z "$xname" ]; then                     # has arg_string, no xname
    pgrep -f -u $USER -x "$pname $arg_string" > /dev/null || daemon -X "$prg $arg_string" -n $prg 
  else                                                                   # no arg_string, has xname
    pgrep -f -u $USER "$pname" > /dev/null || daemon -X "$prg $xname" -n $prg
  fi    

  # variables have to be unset or they'll make a mess in another loop
  unset prg
  unset arg_string
  unset xname
  unset pname
}

I saved the file as ~/bin/autorun.sh. Then I autostart applications that need a GUI from my ~/.xprofile:

source ~/bin/autorun.sh
run_once urxvt "-e newsbeuter" "-name newsbeuter-autorun"
run_once urxvt "-e mutt" "-name mutt-autorun"
run_once urxvt "-e irssi" "-name irssi-autorun"
run_once urxvt "-e mcabber" "-name mcabber-autorun"
run_once urxvt "-e ncmpcpp" "-name ncmpcpp-autorun"
run_once transmission-gtk "" "--name transmission-gtk-autorun"
run_once chromium "" "--name=chromium-autorun" /usr/lib/chromium/chromium

And I put CLI applications into my ~/.profile:

source ~/bin/autorun.sh
run_once mpd "--no-daemon"
run_once devmon "--internal --ignore-device /dev/sda5 --ignore-device /dev/sda6 --ignore-device /dev/sda1 --ignore-device /dev/sda7 --ignore-device /dev/sda8 --no-gui --mount-options nosuid,nodev,noatime" "" "/bin/bash /usr/bin/devmon"

This way LXDM autoruns things from ~/.xprofile and ~/.profile on login and the bash login shell (i.e. logging in without xorg) only starts applications in ~/.profile (make sure you don't have ~/.bash_profile or ~/.bash_login for the bash login shell to read ~/.profile - if so just move the contents of ~/.bash_profile or ~/.bash_login to ~/.profile and delete those files. Also I autorun the applications with daemon - this is to make sure that the autorun applications don't get killed on logging off (I only manually stop the only the GUI applications before rebooting or halting).

Last edited by C5OK5Y (2012-08-19 16:53:51)

Offline

Board footer

Powered by FluxBB