5

I have this little script. When I start it and the terminal has the focus Ctrl+C stops feh and then the while loop starts feh again. I am expecting that, as long as the terminal(!) has the focus, Ctrl+C interrupts the while loop and kills feh as well.

#!/bin/bash

sleep 2
while true; do
feh --cycle-once -zD $1 *.png
done

Can someone tell me how I can get that preferred behavior here?

  • 1
    What's $1 supposed to be ? – Sergiy Kolodyazhnyy May 1 '15 at 19:09
  • 3
    I believe Ctrl+C just kills feh, but not the while loop. You can use Ctrl+Z to stop and then pkill scriptname – Sergiy Kolodyazhnyy May 1 '15 at 19:19
  • 1
    Does SIGQUIT (CTRL + \\ ) work? If not you need to kill the process using SIGKILL – heemayl May 1 '15 at 20:21
2

Try this:

#! /bin/bash

sleep 2
while feh --cycle-once -zD $1 *.png; do :; done

This way, the cycle will end when feh exits with a nonzero status (as it does when you terminate it).

| improve this answer | |
  • 1
    : is the same as true, but there's a typo in the post: there needs to be a space between do and :. – egmont May 3 '15 at 11:52
1

Trap the INT signal, eg: Ctrl+C

more infos here

trap trapint 2
function trapint {
    exit 0
}

In your code like this:

#!/bin/bash

trap trapint 2
function trapint {
    exit 0
}

sleep 2
while true; do
feh --cycle-once -zD $1 *.png
done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy