Created
June 25, 2024 13:42
-
-
Save ericek111/d763618f4ab5c7ae46a36fd6858272ec to your computer and use it in GitHub Desktop.
Bash script to start a long-running process at a specified time, for some set time period
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ -z "$1" ]; then | |
echo "Usage: $0 (start date and time, e. g. 2024-02-10 00:10:15)" | |
exit 1 | |
fi | |
START_DATE="$(date -d "$1" +'%s')" | |
if [ $? -ne 0 ]; then | |
echo "Invalid date format! Check usage..." | |
exit 1 | |
fi | |
# When to stop the recording, e. g. '+30 minute' for half an hour | |
END_DATE="$(date -d '+150 minute' +'%s')" | |
FREQ=3650000 | |
SAMPLE_RATE=6048000 | |
DECIM=16 | |
WAV_RATE=$((SAMPLE_RATE / DECIM)) # Should be an integer. | |
delayTime=$(( "$START_DATE" - "$(date +'%s')" )) | |
echo "==================== Will start in $delayTime seconds..." | |
if [[ "$delayTime" -gt 0 ]]; then | |
sleep "$delayTime" | |
fi | |
isDone="no" | |
trap ctrl_c SIGINT | |
function ctrl_c() { | |
echo "==================== Ctrl+C pressed! Exiting..." | |
isDone="yes" | |
} | |
cd "$(dirname "$0")" | |
PID_FILE="./rxtool.pid" | |
RXTOOL_PID="" | |
function start_rxtool { | |
OUT_FILE="./baseband_${FREQ}_""$(date +'%Y-%m-%d_%H-%M-%S')"".raw" | |
echo "==================== Starting recording to $OUT_FILE" | |
# We need to trap again in a subshell. | |
( trap '' INT ; miri_sdr -f $FREQ -s $SAMPLE_RATE -G 1,0,0,15 - & echo $! >"$PID_FILE" ) \ | |
| csdr convert -i s16 -o float \ | |
| csdr firdecimate $DECIM 0.08 -w hamming \ | |
| csdr convert -i float -o s16 \ | |
> "$OUT_FILE" & | |
RXTOOL_PID="$(cat "$PID_FILE")" | |
rm "$PID_FILE" | |
} | |
start_rxtool | |
while true; do | |
if [[ "$(date +'%s')" -lt "$END_DATE" ]] && [[ "$isDone" == "no" ]]; then | |
# We should be still recording. Check if the process has died. | |
if ! kill -0 "$RXTOOL_PID" >/dev/null 2>&1; then | |
# Process stopped, restart. | |
start_rxtool | |
fi | |
sleep 1 | |
else | |
echo "==================== Recording finished." | |
# Wait for the process to end... | |
while kill -0 "$RXTOOL_PID" >/dev/null 2>&1; do | |
kill "$RXTOOL_PID" | |
sleep 1 | |
done | |
break | |
fi | |
done | |
# split -b 4000M -d -a 1 'baseband_3650000_2024-02-10_05-45-00.raw' baseband_split_ | |
# sox -t raw -e signed-integer -b 16 -c 2 -r $WAV_RATE - ./bb.wav |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment