1

I'm currently trying to extract all the information I need from the .desktop files.

The below is working GOOD ENOUGH but there is one drawback.

Because sometimes the EXEC line in the .desktop file spawns additional processes I'm having to run each file to get the expected process name becuase then I can kill this at a later time.

This is fine for now, however sometimes the GUI flashes up for a second and then closes after I issue the kill command.

My question is, is there an easy way to run commands with no gui that will work for all cases.

Is there a way to run commands whilst hiding the GUI completely?

My script

#!/bin/bash
for filename in /usr/share/applications/*.desktop /var/lib/snapd/desktop/applications/*.desktop; do

    #try and get command line
    CMD=$(grep -Po "(?<=^Exec=).+\s" "$filename" | tail -1)
    $CMD &>/dev/null &
    PID="$!"

    #if no pid or comand found try another approach
    if [[ -z "$CMD" ]] || [[ -z "$PID" ]]  
    then
        CMD=$(grep '^Exec=' "$filename" | tail -1 | sed 's/^Exec=//')
        $CMD &>/dev/null &
        PID="$!"
        if [[ -z "$CMD" ]] || [[ -z "$PID" ]] 
        then
            # if nothing then skip --- TODO fix this 
            continue
        fi
    fi

    name=$(ps --no-header $PID | awk '{print $5}')
    
    
    if [[ -z "$name" ]]
    then
        :
        #echo $CMD
        #echo $filename has no name
    else
        #extract the categories from the file
        categories=$(grep '^Categories' "$filename" | tail -1 | sed 's/^Categories=//' | sed 's/%.//' | sed 's/^"//g' | sed 's/" *$//g')
        #get icon location
        icon=$(grep '^Icon' "$filename" | tail -1 | sed 's/^Icon=//' | sed 's/%.//' | sed 's/^"//g' | sed 's/" *$//g')
        
        if [[ $icon == /* ]] 
        then
            icons=$icon
        else
            icons=$(find "/usr/share/icons/" -name "*$icon.*")
        fi

        #get icon file paths

        echo {"pid":$PID, "name":$name, "filename":$filename, "cmd":$CMD, "categories":$categories, "icons":$icons}
    fi

    kill -SIGTERM $PID &>/dev/null
    #echo KILLING 
done

1 Answer 1

1

I've found a solution - not sure if there is a better way BUT

Here

Use xvfb which is a virtual frame buffer.

Search Results Featured snippet from the web Xvfb (short for X virtual framebuffer) is an in-memory display server for UNIX-like operating system (e.g., Linux). It enables you to run graphical applications without a display (e.g., browser tests on a CI server) while also having the ability to take screenshots.

Install ubuntu etc

sudo apt-get install xvfb

To use... Create the buffer:

Xvfb :100 &

Then set the display to the pseudo display

export DISPLAY=:100

Everything you run in that terminal session now appears to run hidden.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .