while sleep 1; do
pgrep Polo || {
kill `pidof Polo` >/dev/null
/var/bin/polo_start.sh start &
echo `date` R >> /var/log/Check_Polo .log
}
done
exit 0
well , i use that script as a watchdog in my box (with has just 32 mb memory) which check every second if Polo binary is not running , it would start it. this works functionally right here.
but the problem is , it always use memory for sleep 1 command (since my box has a few memory and this watchdog is always running so it's really important for me to save memory of them ) as in top command always shows:
Command %MEM
sleep 1 4.7%
[pgrep] 5 %
i need expert advice , how to change or improvw my script (esp those command) to would not use memory for sleep 1, ... or on the other words use least memory resources. would really appreciate any helps.
P.s : Linux 2.6.23.17_stm23_A21 #1 PREEMPT sh4 GNU/Linux
and pgrep , ... comes from busybox.
Best Regards.
As an alternate to sleep, have you considered using ping? Read the man page appropriate to your OS and define it to ping the local host twice. This usually takes one second, but do check it. Various OS have different options, so you might have one of the following:-
I have no experience with busybox (that I'm aware of) and haven't used linux very much during the past few years. That said, I think you may be overestimating the memory impact of running sleep.
All of sleep's code, the text segment of the executable, is already in ram since the shell running that script is the same exact binary. Since sleep, pgrep, pidof, date, echo, kill, sh, sleep, et al, are all instances of the busybox binary (only thing that differs is the name used to exec them), the kernel is able to run many instances of the binary using only a single copy of their code (the memory management subsystem maps that single copy of the text segment into each process' address space).
By the way, ping is also busybox, so that won't save anything (it will probably hurt a bit, as a ping is more complicated than a sleep).
Resorting to a non-busybox binary that isn't already in use will certainly be counterproductive. Its text segment is not shared with another extant process and so it will consume an amount of ram equal to its size (whereas a shared text segment consumes nearly nothing for each additional instance of that binary).
ok, as told before , after trying to ask or find alternative for sleep which use less memory without any succeed , i decided to delete sleep (or ping ,... ) and run script with while [1] (permanently) , now it doesn't use that 4.7 MEM% of sleep process so this is what i intended to do . (solved somehow with clear questions , but anyway sleep process not used my box memory anymore)
strange part is , probably use while [ 1 ] as permanently loop (which i couldn't see any new row's mem process for that) , use less memory than use while with sleep as top command confirmed!
Best Regards.
Unfortunately that's very inefficient. You will waste a great deal of CPU time and perhaps power. If that's the only thing running on the system though, might be livable.
I continue to think most of that 5% is shared memory, hence not actually wasted. busybox is a big program, but loading it 5 times does not take 5 times the memory.