Bash script in snap -- calling system programs

No trouble calling echo, grep sed, et al.
I get errors calling:
lsusb
and surprisingly:
snap

I need to call snap services to determine whether I need to temporarily stop modem-manager.
Ex: snap services | grep --quiet 'modem-manager\.modemmanager.*[^n]active'

Errors I’m receiving:

/snap/mysnap/x3/bash_script.sh: line 220: snap: command not found
...
/snap/mysnap/x3/bash_script.sh: line 191: lsusb: command not found

I found this message:

I can see adding lsusb as a dependency, but there’s gotta be a way to invoke `snap’ without shipping it as a dependency.

Thanks!

you can not ship or run the snap command itself in a snap (even if you’d ship it, it would not be able to talk to the snapd server outside of your snap) …

snaps do have a builtin snapctl command you can use, this allows a few of the known snap commands to enable snaps to gather info about themselves … among them is snapctl services that lists shipped services and their status:

$ snap run --shell htpdate-daemon.htpdate 
$ snapctl services
Service                Startup  Current   Notes
htpdate-daemon.daemon  enabled  inactive  -
$ exit
$

you will not be able to get any info about other processes outside of your snap (like modemmanager) though …

… that said … if you own a brand store it is possible to grant access to the snapd-control interface and query the snapd API (see the “apps” paragraph):

for lsusb you can just add usbutils as a stage package and add some extra interface plugs (hardware-observe, raw-usb i think)

2 Likes