Posted: Wed Jan 20, 2021 6:20 Post subject: [SOLVED] Script is not working...
Hello,
Not sure if this is the right place to ask this but i am trying to run a script at startup which will detect a string from system log and run few commands...
Code:
#!/bin/sh
dec="wan : WAN is up"
logread -f | while read line; do
if [ "${line#*$dec*}" != "$line" ]; then
sleep 3; stopservice nas; wlconf eth1 down; wlconf eth2 down; wlconf eth1 up; wlconf eth2 up; startservice nas; logger "VAP workaround executed";
fi
done
I know this can be done via .wanup script but for some reason i really need it to work this way.
The file is stored in my samba storage (/opt/config/detect.sh) and i am executing it with ./detect.sh, i made it executable with chmod a+x.
Problem is nothing seems to be happening when i run command
Code:
logger wan : WAN is up
Am i doing it correctly?
Or is there an alternative (better) way of doing this.
Joined: 18 Mar 2014 Posts: 12889 Location: Netherlands
Posted: Wed Jan 20, 2021 10:09 Post subject:
I will transfer your question to the advanced networking forum.
I have not looked in detail but I would use something like:
Code:
grep -q 'WAN is up' /var/log/messages && echo "WAN is up"
But there are other ways to see if the WAN is up, I personally use a ping test via the WAN like:
Code:
PINGIP="8.8.8.8"
INTERFACE=$(get_wanface)
while ! ping -qc1 -W6 -n $PINGIP -I $INTERFACE &> /dev/null; do
There is also an NVRAM variable "wanup"
Take note if you use a script from storage then the script is probably not executed from startup as it takes often more than 10 seconds before the storage is on line.
Recent builds have a utility: is-mounted.sh which waits till the storage is up, it works like:
Code:
is-mounted.sh /opt
Quote:
# wait until directory is mounted and writable
# usage: is-mounted.sh /name-of directory
# default is /jffs
Joined: 04 Aug 2018 Posts: 1447 Location: Appalachian mountains, USA
Posted: Thu Jan 21, 2021 16:11 Post subject:
I have multiple sleep/grep loops in my startup code to watch the log for events, but the OP suggested the construct logread -f | while read line; do... ; done, which is actually much nicer because the loop body is invoked whenever a new line appears in the log. There is no arbitrary sleep time, so synchronization would be tighter.
I'm still on 44048, as I have Apple clients talking to one of those Linksys/Marvell wonderrouters, but I see that even so, logread is there (as a symbolic link to busybox, like other built-ins). However, any attempt to use it yields
logread: can't find syslogd buffer: No such file or directory
So I'm curious. Is it usable only in later builds? Or not at all? Or "coming soon to a router near you"? _________________ 2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.
Joined: 04 Aug 2018 Posts: 1447 Location: Appalachian mountains, USA
Posted: Thu Jan 21, 2021 16:36 Post subject:
egc wrote:
I think you have to start syslogd first with something like (do not know the exact syntax):
/sbin/syslogd -C24
Sorry... I should have said: syslogd is already running. ps shows it was started as syslogd -Z -L. I checked those args and learned that -L requests local logging (not just over the network), and -Z causes times in log entries to be shown in the local timezone. _________________ 2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.