I've written a script to reboot my router every other day. It works as intended when I test it out manually but doesn't work properly in cron.
#!/bin/bash
c=$(ls -a /tmp | grep -o ".daycount*" | wc -l)
if [ "$c" -lt 2 ]; then
let c="$c"+1
touch /tmp/.daycount"$c"
exit
else
rm /tmp/.daycount*
sshpass -f /home/username/.sshpasswrd ssh [email protected] reboot
exit
fi
When grep find no files called .daycount* it creates one called .daycount1
Everyday it would increment by 1 until it hits the "-lt #" threshold, where it would delete all the .daycount* and then reboot my router. In this case, every three days.
It works as intended when I run it manually, but when I run it in cron, it does all the creating/deleting of the files but won't reboot my router.
I've tested it many days in a row and when I check the uptime of the router it has not reset to zero when I would expect it to.
letisn't a shell command, and...), so the question isn't why it doesn't work from cron, the question is how it manages to work manually. shellcheck.net will point out some (but not all) of the problems. BTW, does Asuswrt-Merlin even include bash, or just some more basic shell?letis a bash command, where each argument is executed as arithmetic expression. Here it is used with wring syntax.rm /tmp/.daycount *will delete both/tmp/.daycountand all files in the current directory that don't start with.. You should double-check that this is what you intend to do.set -xat the beginning of the script, and redirect stderr to a file. Then you'll see a trace of the script's execution and its errors in the file, which should help debug it.