Help with creating a shell script for Noisetorch (either bash or fish)

Hello everyone. For some reason, the systemd unit of my microphone keeps changing, so I can't make a systemd service depending on a specific unit. I'm trying to make a shell script that gets the systemd unit of my microphone and adds that to a particular systemd service. To paint the picture:

systemctl list-units --type=device | grep RODE_NT-USB

sys-devices-pci0000:00-0000:00:01.3-0000:02:00.0-usb1-1\x2d2-1\x2d2:1.0-sound-card3-controlC3.device    loaded active plugged RODE_NT-USB

I then need to take this first part, sys-device-...-controlC3.device, and add it to the systemd service.
The part after Requires= and After= should be replaced with this new string.

Description=Noisetorch Noise Cancelling
# Depending on if you're using pulseaudio or pipewire, uncomment the correct line
# After=pulseaudio.service

ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/noisetorch -i -s alsa_input.usb-RODE_Microphones_RODE_NT-USB-00.analog-stereo
ExecStop=/usr/bin/noisetorch -u


My bash-fu isn't quite up to par, so I think this would be a nice place to start :slight_smile:

garuda-inxi just in case

  Kernel: 5.18.8-zen1-1-zen arch: x86_64 bits: 64 compiler: gcc v: 12.1.0
    parameters: BOOT_IMAGE=/@/boot/vmlinuz-linux-zen
    root=UUID=1e990d50-f7c1-4ded-a883-784141da6847 rw [email protected]
    rd.udev.log_priority=3 vt.global_cursor_default=0
    systemd.unified_cgroup_hierarchy=1 loglevel=3 quiet radeon.dpm=0
  Desktop: KDE Plasma v: 5.25.2 tk: Qt v: 5.15.5 wm: kwin_wayland vt: 2
    dm: SDDM Distro: Garuda Linux base: Arch Linux
  Type: Desktop Mobo: ASUSTeK model: PRIME B350-PLUS v: Rev X.0x
    serial: <superuser required> UEFI: American Megatrends v: 3401
    date: 12/04/2017
  Info: model: AMD Ryzen 5 1600 bits: 64 type: MT MCP arch: Zen
    built: 2017-19 process: GF 14nm family: 0x17 (23) model-id: 1 stepping: 1
    microcode: 0x8001129
  Topology: cpus: 1x cores: 6 tpc: 2 threads: 12 smt: enabled cache:
    L1: 576 KiB desc: d-6x32 KiB; i-6x64 KiB L2: 3 MiB desc: 6x512 KiB
    L3: 16 MiB desc: 2x8 MiB
  Speed (MHz): avg: 3254 high: 3493 min/max: 1550/3500 boost: disabled
    scaling: driver: acpi-cpufreq governor: performance cores: 1: 3493 2: 3493
    3: 3493 4: 3493 5: 2986 6: 3480 7: 2948 8: 2873 9: 3019 10: 2796 11: 3493
    12: 3492 bogomips: 83838
  Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm
  Type: itlb_multihit status: Not affected
  Type: l1tf status: Not affected
  Type: mds status: Not affected
  Type: meltdown status: Not affected
  Type: mmio_stale_data status: Not affected
  Type: spec_store_bypass
    mitigation: Speculative Store Bypass disabled via prctl
  Type: spectre_v1
    mitigation: usercopy/swapgs barriers and __user pointer sanitization
  Type: spectre_v2 mitigation: Retpolines, STIBP: disabled, RSB filling
  Type: srbds status: Not affected
  Type: tsx_async_abort status: Not affected
  Device-1: AMD Vega 10 XL/XT [Radeon RX 56/64] vendor: Sapphire
    driver: amdgpu v: kernel arch: GCN 5 process: GF 14nm built: 2017-20 pcie:
    gen: 3 speed: 8 GT/s lanes: 16 ports: active: DP-1,DP-2
    empty: HDMI-A-1,HDMI-A-2 bus-ID: 0a:00.0 chip-ID: 1002:687f
    class-ID: 0300
  Display: wayland server: v: with: Xwayland v: 22.1.2
    compositor: kwin_wayland driver: X: loaded: amdgpu unloaded: modesetting
    alternate: fbdev,vesa gpu: amdgpu d-rect: 4480x1080 display-ID: 0
  Monitor-1: DP-1 pos: right res: 2560x1080 size: N/A modes: N/A
  Monitor-2: DP-2 pos: primary,left res: 1920x1080 size: N/A modes: N/A
    renderer: AMD Radeon RX Vega (vega10 LLVM 14.0.6 DRM 3.46 5.18.8-zen1-1-zen)
    v: 4.6 Mesa 22.1.2 direct render: Yes
  Device-1: AMD Vega 10 HDMI Audio [Radeon 56/64] driver: snd_hda_intel
    v: kernel bus-ID: 1-2:2 pcie: gen: 3 chip-ID: 19f7:0003 speed: 8 GT/s
    class-ID: 0300 lanes: 16 bus-ID: 0a:00.1 chip-ID: 1002:aaf8
    class-ID: 0403
  Device-2: AMD Family 17h HD Audio vendor: ASUSTeK driver: snd_hda_intel
    v: kernel pcie: gen: 3 speed: 8 GT/s lanes: 16 bus-ID: 0c:00.3
    chip-ID: 1022:1457 class-ID: 0403
  Device-3: RODE Microphones NT-USB type: USB
    driver: hid-generic,snd-usb-audio,usbhid
  Sound Server-1: ALSA v: k5.18.8-zen1-1-zen running: yes
  Sound Server-2: PulseAudio v: 16.1 running: no
  Sound Server-3: PipeWire v: 0.3.53 running: yes
  Device-1: Realtek RTL8111/8168/8411 PCI Express Gigabit Ethernet
    vendor: ASUSTeK PRIME B450M-A driver: r8169 v: kernel pcie: gen: 1
    speed: 2.5 GT/s lanes: 1 port: f000 bus-ID: 04:00.0 chip-ID: 10ec:8168
    class-ID: 0200
  IF: enp4s0 state: up speed: 100 Mbps duplex: full mac: <filter>
  Device-1: ASUSTek Broadcom BCM20702A0 Bluetooth type: USB driver: btusb
    v: 0.8 bus-ID: 1-3:3 chip-ID: 0b05:17cb class-ID: fe01 serial: <filter>
  Report: bt-adapter ID: hci0 rfk-id: 0 state: down
    bt-service: enabled,running rfk-block: hardware: no software: no
    address: <filter>
  Local Storage: total: 2.61 TiB used: 1.94 TiB (74.2%)
  SMART Message: Unable to run smartctl. Root privileges required.
  ID-1: /dev/nvme0n1 maj-min: 259:0 vendor: Samsung
    model: SSD 970 EVO Plus 250GB size: 232.89 GiB block-size: physical: 512 B
    logical: 512 B speed: 31.6 Gb/s lanes: 4 type: SSD serial: <filter>
    rev: 2B2QEXM7 temp: 48.9 C scheme: GPT
  ID-2: /dev/sda maj-min: 8:0 vendor: Samsung model: ST1500LM006 HN-M151RAD
    size: 1.36 TiB block-size: physical: 4096 B logical: 512 B speed: 6.0 Gb/s
    type: HDD rpm: 5400 serial: <filter> rev: 0008 scheme: GPT
  ID-3: /dev/sdb maj-min: 8:16 vendor: Crucial model: CT120BX100SSD1
    size: 111.79 GiB block-size: physical: 512 B logical: 512 B speed: 6.0 Gb/s
    type: SSD serial: <filter> rev: MU01 scheme: GPT
  ID-4: /dev/sdc maj-min: 8:32 vendor: Samsung model: SSD 860 QVO 1TB
    size: 931.51 GiB block-size: physical: 512 B logical: 512 B speed: 6.0 Gb/s
    type: SSD serial: <filter> rev: 1B6Q scheme: GPT
  ID-1: / raw-size: 232.59 GiB size: 232.59 GiB (100.00%)
    used: 166.51 GiB (71.6%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
  ID-2: /boot/efi raw-size: 300 MiB size: 299.4 MiB (99.80%)
    used: 576 KiB (0.2%) fs: vfat dev: /dev/nvme0n1p1 maj-min: 259:1
  ID-3: /home raw-size: 232.59 GiB size: 232.59 GiB (100.00%)
    used: 166.51 GiB (71.6%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
  ID-4: /var/log raw-size: 232.59 GiB size: 232.59 GiB (100.00%)
    used: 166.51 GiB (71.6%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
  ID-5: /var/tmp raw-size: 232.59 GiB size: 232.59 GiB (100.00%)
    used: 166.51 GiB (71.6%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
  Kernel: swappiness: 133 (default 60) cache-pressure: 100 (default)
  ID-1: swap-1 type: zram size: 15.56 GiB used: 2 MiB (0.0%) priority: 100
    dev: /dev/zram0
  System Temperatures: cpu: N/A mobo: N/A gpu: amdgpu temp: 53.0 C
    mem: 47.0 C
  Fan Speeds (RPM): N/A gpu: amdgpu fan: 1219
  Processes: 413 Uptime: 32m wakeups: 0 Memory: 15.56 GiB
  used: 6.55 GiB (42.1%) Init: systemd v: 251 default: graphical
  tool: systemctl Compilers: gcc: 12.1.0 clang: 14.0.6 Packages: 2281
  pacman: 2248 lib: 525 flatpak: 33 Shell: fish v: 3.4.1 default: Bash
  v: 5.1.16 running-in: kitty inxi: 3.3.19
e[1;34mGaruda (2.6.4-2):e[0m
e[1;34m  System install date:e[0m     2021-12-14
e[1;34m  Last full system update:e[0m 2022-07-03
e[1;34m  Is partially upgraded:  e[0m No
e[1;34m  Relevant software:      e[0m NetworkManager
e[1;34m  Windows dual boot:      e[0m No/Undetected
e[1;34m  Snapshots:              e[0m Snapper
e[1;34m  Failed units:           e[0m home-sylvester-RaspberryPi-disks-books.mount home-sylvester-RaspberryPi-disks-camera.mount home-sylvester-RaspberryPi-disks-home.mount home-sylvester-RaspberryPi-disks-shows.mount home-sylvester-RaspberryPi-disks-stash.mount 

Don't mind the failed mounts, that's a separate issue I'm trying to fix.

Can you turn this into a script, and then call the script from your systemd service?

How do you mean?

At first I was thinking you could script the line that identifies the device name and have the output be used as the value in your Requires= and After= lines, but after reading this I'm thinking that can't possibly be the best way:

As mentioned above, a unit may be instantiated from a template file. This allows creation of multiple units from a single configuration file. If systemd looks for a unit configuration file, it will first search for the literal unit name in the file system. If that yields no success and the unit name contains an "@" character, systemd will look for a unit template that shares the same name but with the instance string (i.e. the part between the "@" character and the suffix) removed. Example: if a service [email protected] is requested and no file by that name is found, systemd will look for [email protected] and instantiate a service from that configuration file if it is found.

This makes it sound like you might have some leeway with the device name if you are able to incorporate the '@' character between the variable part of the name and .device.

I'll admit I'm way beyond my depth on this one (the more I read about systemd the more lost I get); hopefully someone with more experience will weigh in.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.