For learning purposes, I wish to change a non-stop script like this:
#!/bin/env bash
while true; do
sleep 3
echo 1 >> /tmp/log
done
I know that Linux will load the recent file into memory so I'm guessing that is the reason why I can't simply change the echo 1 >> /tmp/log
to echo 2 >> /tmp/log
and save the script to see the result.
So I change the script :
#!/bin/env bash
CMD="$0 $@"
handleSigHup() {
echo "Receive single SIGHUP, reloading..."
exec $CMD
}
trap 'handleSigHup' SIGHUP
while true; do
sleep 3
echo 2 >> /tmp/log
done
Now I can use kill -HUP
to see the /tmp/log output:
1
1
1
2
2
2
2...
Here are my questions:
Is my guessing right? If so, can I change some sort of system vars to turn off the buffering and make It work?
My code above will only work once, when I change the script to
echo 3 >> /tmp/log
andkill -HUP
again, It won't output the3
as expect, WHY ?
Thanks all!