Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Currently I use two different while loops to start my window manager, dwm, and the status bar that prints system info to it.

My solution at the moment is to run them consecutively in the same script, like so:

while true; do
        $HOME/Scripts/dwm-status
        sleep 2s
    done &
while true; do
    dwm >/dev/null
done

I have also seen it run as a nested while loop, like this:

while true; do
        while true; do
            $HOME/Scripts/dwm-status
            sleep 2s
        done &
        dwm >/dev/null
    done

The second seems to cause CPU spikes. In terms of efficiency (least call on resources etc.,) what is the best approach to run these two loops and why?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

I'm not sure if I'm pointing out something obvious, but the first way is creating two loops that run forever, while the second is creating an infinite loop that creates infinite loops, so you're going to end up with an ever-increasing number of infinite loops. It creates an infinite loop, runs dwm, and then creates another infinite loop -- the first infinite loop hasn't stopped. Each pass will make another dwm-status loop, even though the others are already running (while true will keep them running indefinitely)

share|improve this answer
    
Thanks Michael: no, it wasn't obvious, more of a suspicion. Does that mean the first approach is the correct one, or is there a more efficient way to do it? –  jasonwryan Jun 12 '11 at 3:21
    
@jasonwryan Well, I'm not sure what you're trying to do, exactly. Why would you need to run your window manager multiple times? I would think you could just background dwm once and then run dwm-status in a loop –  Michael Mrozek Jun 12 '11 at 3:59
1  
It allows you to restart dwm without closing programs. Admittedly, I use it infrequently, but it is handy nonetheless... See: wiki.archlinux.org/index.php/…; –  jasonwryan Jun 12 '11 at 4:10
1  
@jasonwryan Wow, that's fantastically hackish :). Then yes, the first approach is probably good. Although now that I understand the second way, it should only be making a new dwm-status loop every time you kill dwm, which I assume isn't that often, so I'm not sure it's actually the cause of your problems –  Michael Mrozek Jun 12 '11 at 4:14
    
Thanks Michael. –  jasonwryan Jun 12 '11 at 4:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.