Bash script in Debian 11 Bullseye making use of "sed" runs ok from command line but not as a cron
Hi, I'm running Debian GNU/Linux 11 (bullseye) Linux 5.15.84-v8+ #1613 SMP PREEMPT Thu Jan 5 12:03:08 GMT 2023 aarch64 GNU/Linux
In my setting, I have two bash scripts:
A different script fills in myfolder with JPEG files every minute 24 hours a day, non-stop. It's a cronjob and runs flawlessly.
When run from command line in /home/myuser the script below runs flawlessly and creates a file that lists all JPEG files in myfolder between start time and finish time
eg of filename
2023-03-05_1014.jpg
eg of output.txt created from command line myfolder/2023-03-05_1014.jpg
#!/bin/bash
cd /home/myuser/
current_date=$(date +%F)
time_start=2023-03-05_0900
time_finish=2023-03-05_1700
ls myfolder/"$current_date"_*.jpg | sed -n "/myfolder\/$time_start/, /myfolder\/$time_finish/p" > output.txt
When run as a cronjob as user myuser (not root)
00 22 * * * /home/myuser/create_jpg_list.sh 2>&1
output.txt will list every single file in myfolder with every JPEG file listed from 0000 through 2359. That is, the variables in the sed command do not seem to be interpreted.
eg of output.txt created from cronjob
myfolder/2023-03-05_2302.jpg
this line should not show in output.txt because it is out of the time range 9am to 5pm
I have searched online and there are a few threads such as the links below (to name a few) dealing with this scenario but alas https://unix.stackexchange.com/questions/614759/sed-doesnt-work-under-cron https://askubuntu.com/questions/524726/not-able-to-run-sed-on-bash-script https://unix.stackexchange.com/questions/662851/why-this-script-doesnt-work-from-cron-while-it-works-when-invoke-from-my-shell https://www.linuxquestions.org/questions/linux-newbie-8/sed-not-working-from-cron-706865/
Any feedback is welcome as I do not know what else to try.