You are not logged in.
Trying to run a script in the background. I have tried appending '&' to detach it, and also tried creating a systemd user service but both do not work. when running systemd --user start rtlpager.service it runs the script, but no stdout is sent to the desired output but the processes get started. The same happens when appending '&'. All the programs support stdin and out so this theoretically should work and does when run by a user normally (but I just dont want it to take up a tty). If there is no solution I may look at the screen command as I have heard it could solve this.
#!/bin/sh
logFile="POCSAG-153.350.000-$(date +%d-%m-%Y_%H-%M-%S)"
rtl_fm -T -g 25 -M fm -f 153.352M -s 48000 | sox -t raw -esigned-integer -b16 -r 48000 - -t raw -esigned-integer -b16 -r 22050 - | multimon-ng -t raw -a POCSAG512 -a POCSAG1200 -a POCSAG2400 -f alpha -e --timestamp - > ~/media/signals/pocsag/decoded/$logFile[SOLUTION]
Running the script as a system service and making sure the output directory exists is what fixed it for me (that last point being my own oversight).
Last edited by talowicz (2025-11-26 02:09:52)
Offline
both do not work.
How do they not work? Is logfile created but empty? Or it is not created?
when running systemd --user start rtlpager.service it runs the script, but no stdout is sent to the desired output
What is content of rtlpager.service?
Are there relevant journal messages?
Does it work if you replace rtl_fm live stream with recorded file?
Offline
When you run this, what's the state of the involved processes (check "ps aux")?
Some programs don't like being forked and SIGHUP (tried nohup?) or even SIGSTOP as result (though a common workaround is then to close stdin and stdout - what's not an option in this case)
I'll also point out that https://man.archlinux.org/man/multimon-ng.1 doesn't list "-" as shorthand for /dev/stdin
Offline
I'll also point out that https://man.archlinux.org/man/multimon-ng.1 doesn't list "-" as shorthand for /dev/stdin
multimon-ng --help does:
Usage: multimon-ng [file] [file] [file] ...
If no [file] is given, input will be read from your default sound
hardware. A filename of "-" denotes standard input.
Offline
talowicz wrote:both do not work.
How do they not work? Is logfile created but empty? Or it is not created?
talowicz wrote:when running systemd --user start rtlpager.service it runs the script, but no stdout is sent to the desired output
What is content of rtlpager.service?
Are there relevant journal messages?Does it work if you replace rtl_fm live stream with recorded file?
Thanks for the reply, and apologies for not responding for 10 days.
A log file is created in the intended directory, and data is correctly fed into it.
Here is my service file:
[Unit]
Description=Decode POCSAG using rtl_fm, sox and multimon-ng then output to file.
After=network.target
[Service]
ExecStart=/home/user/.local/bin/rtlsdr_pager_rx
Restart=always
[Install]
WantedBy=multi-user.targetI am SSH'ing into the server I am running the service on, then exiting. I have found that it works fine while the ssh session is alive, but when I terminate it and log back on after atleast 10 seconds the service is dead. When grepping journal -b I found nothing pertaining to 'sdr', 'rtl' and 'pager'. I think after my user login ends the service is being terminated for some reason.
Offline
I am SSH'ing into the server I am running the service on, then exiting. I have found that it works fine while the ssh session is alive, but when I terminate it and log back on … the service is being terminated for some reason.
You closed all session of your user => user services are gone.
If you want this to run w/o being logged in, why is this a user service?
Offline
I think after my user login ends the service is being terminated for some reason.
Systemd terminates user's services when user is logged out. You have few options to solve this: either run your script as a system service or enable linger for your user.
But this doesn't explain why the script doesn't work when started from command line in background. Don't you have "KillUserProcesses=yes" in /etc/systemd/logind.conf?
Another issue may be access permissions to SDR device by rtl_tcp. rtl-sdr package uses uaccess tag in udev rules, so SDR device is only available for locally logged in users or members of rtlsdr group.
Offline
I am SSH'ing into the server I am running the service on, then exiting. I have found that it works fine while the ssh session is alive, but when I terminate it and log back on … the service is being terminated for some reason.
You closed all session of your user => user services are gone.
If you want this to run w/o being logged in, why is this a user service?
Yeah that would make sense. Never messed around with systemd services before; I'll make it a system service.
Offline
talowicz wrote:I think after my user login ends the service is being terminated for some reason.
Systemd terminates user's services when user is logged out. You have few options to solve this: either run your script as a system service or enable linger for your user.
But this doesn't explain why the script doesn't work when started from command line in background. Don't you have "KillUserProcesses=yes" in /etc/systemd/logind.conf?
Another issue may be access permissions to SDR device by rtl_tcp. rtl-sdr package uses uaccess tag in udev rules, so SDR device is only available for locally logged in users or members of rtlsdr group.
I am now trying to run the script as a system service with the script placed in '/root/.local/bin/' and the systemd service file placed in '/etc/systemd/system/'. I am certain my udev permission are set correctly as per the file '/etc/udev/rules.d/rtlsdr.rules'. When running as a system service, the service fails with this error:
Nov 24 22:51:41 sdr sh[40418]: Oversampling output by: 1x.
Nov 24 22:51:41 sdr sh[40418]: Buffer size: 8.13ms
Nov 24 22:51:41 sdr sh[40418]: Exact sample rate is: 1008000.009613 Hz
Nov 24 22:51:41 sdr sh[40418]: Sampling at 1008000 S/s.
Nov 24 22:51:41 sdr sh[40418]: Output at 48000 Hz. <---- This should be the last output message if it is successful
Nov 24 22:51:41 sdr sh[40419]: sox FAIL sox: `-' error writing output file: Broken pipe
Nov 24 22:51:41 sdr sh[40418]: Signal caught, exiting!
Nov 24 22:51:42 sdr sh[40418]: User cancel, exiting...
Nov 24 22:51:42 sdr systemd[1]: rtlsdr_pager.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Nov 24 22:51:42 sdr systemd[1]: rtlsdr_pager.service: Failed with result 'exit-code'.Offline
Nov 24 22:51:41 sdr sh[40418]: Output at 48000 Hz. <---- This should be the last output message if it is successful Nov 24 22:51:41 sdr sh[40419]: sox FAIL sox: `-' error writing output file: Broken pipe
Looks like subsequent multimon-ng terminated and sox can't write to its stdin.
I'd suspect that if you run the service as root and have ~ in the script, it is likely expanded to "/root" but /root/media/signals/pocsag/decoded/ path doesn't exist. However, in this case some error message, e.g. "No such file or directory" should appear in the journal. Don't you have "StandardError=" overriden?
It is convenient to set "SyslogIdentifier=rtlpager" in [Service] section of the unit to identify service's output.
Add "set -x" at the beginning of your script to see what exactly is executed. Set "Restart=no" for debug. Check the journal with "journalctl -b -u rtlsdr_pager.service".
Also just for test you can try to remove multimon-ng from the chain, let sox write it's raw output to the file.
Offline
The redirection to
> ~/media/signals/pocsag/decoded/$logFilemight (now) fail unless there's a /root/media/signals/pocsag/decoded/
Can we have a clarification on
But this doesn't explain why the script doesn't work when started from command line in background.
If you just run the script manually in the background "/root/.local/bin/myfancyscript.sh &", no systemd, no logging out, you're sitting there and watching it: does it work or fail as well?
Offline
Got it working the way I wanted using a systemd system service and correctly outputting it to a directory that exists (complete oversight on my end with the ~ expansion).
As you requested seth, I have ran the script in the background manually and everything is working correctly.
Learned a few core concepts from this so thanks for the time of all who responded - I will mark as solved and edit to add a fix. ![]()
Offline