r/xmonad•
azy2

Code runs fine in shell but not xmobar

I hope this is the right subreddit for this and you guys can help me out. I have a script that runs in xmobar that I wrote in order to display my internet connection status. If I'm plugged in it'll display Wired and if I'm connected to wifi it will display the SSID along with some bars that indicate how strong the connection is. For instance my wifi is called petrel and I'm sitting next to the router so it looks like this: Petrel ▂▄▆█ When I run this in my shell (zsh, also works in bash though) it outputs fine. However, when I tell xmobar to execute the program it spits an error onto my bar Could not execute command /home/ben/bin/wireless

It mysteriously doesn't work! Here's the code being run:

#!/bin/python
import re
from plumbum import local

sudo = local['sudo']
nmclid = sudo['nmcli', 'd']()
nmclidwifi = sudo['nmcli', 'd', 'wifi']()


def grepLine(pattern, lines):
    for line in lines.split('\n'):
        search = re.search(pattern, line)
        if search:
            return line

wired = grepLine("ethernet", nmclid)

if "connected" in wired:
    print("Wired")
else:
    ssid = grepLine("connected", nmclid)
    ssid = re.search("connected\s+\S+", ssid).group()
    ssid = re.search("\s+\S+", ssid).group()
    ssid = re.search("\S+", ssid).group()

    bars = grepLine("\*\s+" + ssid, nmclidwifi)
    bars = re.search("[_▂▄▆█]+", bars).group()

    print(ssid + " " + bars)

If I have an ethernet cord plugged in it prints Wired just fine and there are no issues, which narrows the problem down to that last else clause. Why would xmobar fail here when my shell doesn't? Does xmobar not use some form of exec?

edit. I think I've narrowed it down to the command

sudo nmcli d wifi

although why that fails is beyond me, considering

sudo nmcli d

works perfectly

Posted Aug 3, 2015, 10:58 PM UTC · edited

46
seschwar•

I think the problem is your usage of sudo. sudo needs to ask for a password. It can do so in the terminal, but it can't when you run the script from xmobar.

Is sudo really required when you are just getting status information form NetworkManager?

If you really need to run sudo without terminal access you can specifiy a program to be used for prompting for the password. Use the command line arguments -A or --askpass, or the environment variable SUDO_ASKPASS to do so (see manual page for the details).

Alternatively you can whitelist just these single two commands to not require a password when run with sudo:

azy2 ALL=(ALL) NOPASSWD: /usr/bin/nmcli d, /usr/bin/nmcli d wifi
1
azy2OP•

I did figure out sudo isn't required here but I had already whitelisted those commands. So that's not the problem. I took sudo out and it's still not running in xmobar

1
A_Wild_Spoink•

I'm pretty sure xmobar has issues with displaying unicode.

1
azy2OP•

That's the thing I can print the bars directly and they show up. That's why I'm so confused

1
A_Wild_Spoink•

Did you make sure to mark the script as executable?

1
azy2OP•

yup

1