Start/stop a shell script loop

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

  1. macrumors newbie

    May 28, 2013
    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 '' 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 '' every few seconds? In my pipedream, I'd be able to start and stop it from an icon on my menu bar... any hints?

    #! /usr/bin/env bash
    for (( i = 0 ; i < 10000 ; i++))
       now=`date +%s`
       left=`expr ${dt} - \( ${now}  % ${dt} \)`
       sleep $left
       open "/Users/hakonandersson/Documents/Traktor automated screencaps/"
    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,
  2. macrumors 604


    Nov 26, 2007
    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

    May 28, 2013
    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