Run commands in background

Sometimes it would be nice when any shell-commands can run in background and when it exits, send a "signal" to awesome - without hanging awesome! Maybe if you have a slow internet connection and want run a simple translation via google translate your awesome will hangs...

This function can do only shell-commands, lua/awesome-commands NOT.

background_timers = {} function run_background(cmd,funtocall) local r = io.popen("mktemp") local logfile = r:read("*line") r:close cmdstr = cmd .. " &> " .. logfile .. " & "                                        local cmdf = io.popen(cmdstr) cmdf:close background_timers[cmd] = { file = logfile, timer = timer{timeout=1} }                                                                                 background_timers[cmd].timer:add_signal("timeout",function                           local cmdf = io.popen("pgrep -f '" .. cmd .. "'")                                 local s = cmdf:read("*all")                                                        cmdf:close                                                                       if (s=="") then                                                                        background_timers[cmd].timer:stop                                                local lf = io.open(background_timers[cmd].file)                                    funtocall(lf:read("*all"))                                                         lf:close            io.popen("rm " .. background_timers[cmd].file)                                                                   end                                                                            end) background_timers[cmd].timer:start end

An usage example:

run_background("ping -c 5 www.google.hu",function(txt)                                 naughty.notify({text=txt})                                                       end)

This example will shows the output of command `ping' after 5 seconds (because of parameter -c 5).