How to run a simple script

Hi,
I would like to add a script to crontab. Ping a host and if not reply restart the local service.

> #!/bin/sh
> # Configuration
> ZEROTIER_PEER="10.0.0.3"
> PING_TIMEOUT=3
> # Check ZEROTIER peer reachability
> ping -c 2 -W $PING_TIMEOUT $ZEROTIER_PEER > /dev/null
> if [ $? -ne 0 ]; then
>     # ZEROTIER peer unreachable, restart Zerotier
>     /etc/init.d/zerotier restart
>     # Log the restart event (optional)
>     logger -t "zerotier_restart.sh" "ZEROTIER peer unreachable, restarting Zerotier"
> fi

I created this file zerotier_restart.sh and copy it to /etc/zerotier/
then I did: chmod +x /etc/zerotier/zerotier_restart.sh

but the output is:


root@openwrt:/etc/zerotier# ./zerotier_restart.sh
-ash: ./zerotier_restart.sh: not found

where am I going wrong?

Are those > on every line part of the script, or is it just the way you pasted it ?

just pasted from notepad++

You're not answering the question...

Does the file on your Openwrt device contain those > on every line ?

do not contain >
this is the script in .sh file:

#!/bin/sh
# Configuration
ZEROTIER_PEER="10.0.0.3"
PING_TIMEOUT=3
# Check ZEROTIER peer reachability
ping -c 2 -W $PING_TIMEOUT $ZEROTIER_PEER > /dev/null
if [ $? -ne 0 ]; then
    # ZEROTIER peer unreachable, restart Zerotier
    /etc/init.d/zerotier restart
    # Log the restart event (optional)
    logger -t "zerotier_restart.sh" "ZEROTIER peer unreachable, restarting Zerotier"
fi

Most likely, the file was created under Windows.
Try to fix it this way:

sed -i -e "s/\r//g" /etc/zerotier/zerotier_restart.sh
2 Likes

Thank you !!

1 Like

Doesn't dos2unix exist on Openwrt ?

:slight_smile:

uhhmmmm..

I checked the script is running correctly, but I scheduled this task to system crontab:

#
*/5 * * * * /etc/zerotier/zerotier_restart.sh
#

the log shows error:

root@openwrt:~# logread -e cron
Sat May 24 10:07:20 2025 cron.err crond[18449]: crond (busybox 1.35.0) started, log level 5
Sat May 24 10:10:00 2025 cron.err crond[18449]: USER root pid 18645 cmd /etc/zerotier/zerotier_restart.sh
Sat May 24 10:15:00 2025 cron.err crond[18449]: USER root pid 19038 cmd /etc/zerotier/zerotier_restart.sh

Pretty sure the cron.err is normal, even if it executed correctly.

1 Like

You are right, log show error but is executed :+1:

If you think it's worth installing it just to convert one file, yes, why not :wink:

You can get rid of those messages that fill up the logs.

uci set system.@system[0].cronloglevel="9"
uci commit system
/etc/init.d/cron restart
2 Likes

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