Help with small bash script

Hey,

I am new to bash scripting and only understand the very basics. i want to test some wireless access points loco m5’s and I was looking to put them in my workshop and run iperf3 every 5 mins to see how well they hold up.

I have just ran a very basic iperf3 -c 10.0.0.20 >> loco.log but I want to add before every test "The test started at “date”. a line break then the iperf test below and then the same again 5 mins later when its on a cron job.

Any ideas on how i would do this?

Thanks
Dave

#!/bin/sh
echo "Test started on $(date)" >> loco.log
echo >> loco.log
iperf3 -c 10.0.0.20 >> loco.log
echo >> loco.log
echo "Test stopped on $(date)" >> loco.log

The crontab would look like

*/5 * * * * /path/to/script.sh
2 Likes

brilliant i understand now

i was thinking that i should have it all done in 1 line for everything but it makes sense how it works when i see it.

great thank you for the help as always!

I wrote that in a rush, but it’s probably better to make your script spit everything to stdout (remove all the >> loco.log) and instead redirect the script to the /full/path/to/loco.log in crontab.

And the cron job will just output the loco.log to whichever working directory it’s running from, which ain’t good, unless you have an explicit cd /to/directory in the script (which I didn’t put in). And make sure to make the script executable by the user that’ll have that script in its cron.

loco.sh

#!/bin/sh
echo "Test started on $(date)"
echo
iperf3 -c 10.0.0.20
echo
echo "Test stopped on $(date)"

crontab -e

*/5 * * * * /path/to/script.sh >> /var/log/loco.log 2>> /var/log/loco.err

And chmod +rx loco.sh (with the optional chown other-user loco.sh).