Start/stop a shell script loop

Discussion in 'Mac Programming' started by hakonandersson, May 28, 2013.

  1. macrumors newbie

    Joined:
    May 28, 2013
    #1
    Hi all,

    Many thanks in advance for any help you can provide. I've built an automator app which takes a screenshot (screencapture), crops out 3 seperate images (imagemagick), runs gocr over those images to capture text, then sends that text via OSC to another device.

    This is working fine. I want this program to loop every 3-5 seconds.. it takes around 1.5-2 seconds max to finish.

    So, I looked around and found the below shell script online which allows me to
    repeatedly run the program every 4 seconds (dt=4). This is working ok, but after I trigger that in a terminal shell - even if I shutdown terminal, the process is still executing and my 'crop_screen_caps.app' keeps running every 4 seconds.

    This isn't a huge issue - but I'd rather a nicer way for me to start and stop the loop... or perhaps there is an easier way for me to trigger 'crop_screen_caps.app' every few seconds? In my pipedream, I'd be able to start and stop it from an icon on my menu bar... any hints?

    Code:
    #! /usr/bin/env bash
    dt=4
    for (( i = 0 ; i < 10000 ; i++))
      do
       now=`date +%s`
       left=`expr ${dt} - \( ${now}  % ${dt} \)`
       sleep $left
       open "/Users/hakonandersson/Documents/Traktor automated screencaps/crop_screen_caps.app"
      done
    
    Thanks for any suggestions you can provide in terms of a cleaner way to trigger my .app file every few seconds, and be able to stop it if I want to.

    Best regards,
    Hakon.
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Doesn't Automator provide loops? Why do you need to use a shell script at all? (I may be mistaken, I haven't tried doing anything with Automator in years, but I seem to recall being able to set it up to run a task periodically...)
     
  3. thread starter macrumors newbie

    Joined:
    May 28, 2013
    #3
    Hi ArtOfWarfare,

    Ah-ha, so it does :) Ok, so I've added a 3 second pause, then a loop function at the end with an insane amount of minutes to run for..

    And lo and behold, the workflow cog is now up the top in my menu.

    Many thanks mate! The answer is most of the time a lot simpler than first thought. Cheers.
     

Share This Page