0

I have the following code that monitors a directory for any new pictures that are placed into it by a camera. It's not working in it's current state, but the idea is that when it detects a new picture being created in the directory, it displays it in feh fullscreened, wait's an amount of time and then kills the process to display the next one.

Currently, it hangs at the step of calling feh and doesn't step any further until I manually escape out of feh, but then the step to kill feh results in no feh process found.

Ideally, I'd love to just be able to refresh feh and have it display a new image for every image found, instead of killing it and calling it again but sadly each of the new images has a different file name.

#!/bin/bash  
DIR="/path/to/pictures"  
inotifywait -m -r -e create "$DIR" | while read path action file; 

do  
 feh -F $DIR$file 
 sleep 10
 kill -9 ($pidof feh)
done
  • 1
    Add a & to feh .... – jake Jan 26 at 2:04
1

The main thing is, that you need to add a & to background feh. Otherwise it will wait for feh to exit.

This should work, but is maybe not the most elegant solution:

#!/bin/bash  
DIR="/path/to/pictures"  
inotifywait -m -r -e create "$DIR" | while read path action file; 

do
  if [ "$pid" != "" ]; then
    kill -9 "$pid"
  fi
  feh -F "$DIR$file" &
  pid="$!"   # $! is a shell variable containing the PID of the last process started
  sleep 10   # You only need this, if you want to show the picture for minimum time
done

You actually don't need to get the PID, as this will work as well:

do
  pidof feh && pkill feh # this will check if there's a process called feh and kill it
  feh -F "$DIR$file" &
  sleep 10 
done

EDIT:

You could also use geeqie which supports remote control:

do
  sleep 10 #only needed for delay
  geeqie -r -t -fs "$path$file"
done
  • I'm super new to bash. What does declaring pid="$!" do? – T. Cubbin Jan 26 at 3:56
  • "$!" is the PID of the last executed program. – jake Jan 26 at 9:20
  • +1 to @jake or the answer (add & at the end of the command to background it) but you forgot to add the sleep 10 delay in the loop that the OP wanted in the question. – Roger Jones Jan 26 at 13:31
  • @RogerJones I added it. Thought the sleep 10 was maybe some workaround. – jake Jan 26 at 14:30
  • Fair enough, although with out it would the script not kill feh as soon as it's launched? OP said "...displays it in feh fullscreened, wait's an amount of time and then kills the process..." – Roger Jones Jan 26 at 16:37

Your Answer

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

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