need help on bash scripting

Discussion in 'Mac Programming' started by tonkpils, Dec 9, 2013.

  1. tonkpils macrumors newbie

    Joined:
    Dec 9, 2013
    #1
    Hello all, i am realatively new to this type of programming on Mac, and need some help...

    here is the story:

    I have a script to record streams with rtmpdump.
    As it refer to 3 different channels, i have made 3 scripts, one for each channel.
    I run each of them from the terminal, and in the script i have set a variable in the name of the output file that is the date and timing of the begin of the recording.

    What i want to add to this script is:
    a piece of code that check the file size of the file being recorded.

    The check should be done every 5 minutes.

    If the file is not increasing in size, then restart the script.

    To run these script when i am not at home, i also need to know what is the best solution:
    An application to schedule task?
    another script to run the day of the recording, with instructions on the hour to start?
    ...


    will you point me to the right directions in order to achieve this?

    thanks in advance for your help!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    First, what scripting language are your scripts written in?

    Second, post an example of one.
     
  3. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #3
    One way to accomplish this is to use crontab. crontab is a service that exists in OS X (and other Unix systems) which allows you to schedule a command to execute at a specific time each day, and/or at a specific interval.

    You could write a script that does the following (all it monitor.sh):
    1. If the file has not been updated in the past 5 minutes, use "kill -9" to kill the associated process.
    2. If the process is not running, launch the script.

    Then configure crontab to run the monitor.sh script every 5 minutes. This would have the effect of ensuring that progress must be made otherwise the processing script is killed. And, in any case where the processing script is not executing, start up the processing script (including the case where it was killed due to lack of progress).

    You can get a list of current processes from the "ps" command. Then "grep" the output to find your processing script.
     
  4. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #4

    First off cron to start the job off at your prescribed time, cue the nitpickers whom every time I mention it seem to pop up to say but cron is depreciated use this whatever Apple has that is supposed to replace it totally some time in the future... To your size problem the easiest way to do it as I can see is to use the blocks that the file is taking up on the disk, this should always be increasing so to get that use similar replacing with your file name.

    Code:
    MacUser2525:~$ ls -ls metadata.txt | cut -d ' ' -f1
    8
    
    That gets you your blocks so in your script after you start the stream this.

    Code:
    x=`ls -ls metadata.txt | cut -d ' ' -f1`
    
    To make use of this with your delay something like this.


    Code:
    while x >= 1 ; do
         sleep 300
         y=`ls -ls metadata.txt | cut -d ' ' -f1`
    done
    
    So now you have the two variables you need the starting and after five minutes values for the blocks used you now need to check if it is bigger then do something to tell the system it is either still running or stopped and needs to be restarted.

    Code:
    while x >= 1 ; do
         sleep 300
         y=`ls -ls metadata.txt | cut -d ' ' -f1`
    # If block count has increased then echo it is running else say it is not then exit script.
         if y >= x ; then
              echo "1" > /path/to/file/running.txt
              x=y
         else
              echo "0" > /path/to/file/running.txt
              exit 0
         fi
    done
    
    Since you will want the recording to stop after a set period I would think put in a check for that time period elapsed.

    Code:
    while x >= 1 ; do
         sleep 300
         y=`ls -ls metadata.txt | cut -d ' ' -f1`
    # If block count has increased then echo it is running else say it is not then exit script.
         if y >= x ; then
              let Count=Count+1
              if Count <=12
                   echo "1" > /path/to/file/running.txt
                   x=y    
              else
    # Tell system processing is complete
                   echo "2" > /path/to/file/running.txt
                   exit 0
              fi
         else
    # Tell system processing is stopped needs restart
              echo "0" > /path/to/file/running.txt
              exit 0
         fi
    done
    
    This should stop it after an hour. For a second file to check on the running or not then.

    Code:
    #!/bin/bash
    sleep 301
    is_running="$(cat /path/to/file/running.txt | head -c 1)
    
    # while it is running keep checking to see if it needs restart or stopping of this script
    while $is_running = 1 ; do
         sleep 300
         is_running2="$(cat /path/to/file/running.txt | head -c 1)
         if $is_running2 = 2
              exit 0
         else
            /path/to/file/for/your/audio/recording.sh &
         fi
    done
    
    Think I have it covered in those two if not at least it gives you something to tweak..
     
  5. tonkpils thread starter macrumors newbie

    Joined:
    Dec 9, 2013
    #5
    thanks a million! i'll start playing around in a few minutes... :)

    i'll let you know the outcome. :)
     
  6. tonkpils thread starter macrumors newbie

    Joined:
    Dec 9, 2013
    #6
    It works... :) i have what i was looking for.
    but now i am changing this to something in PHP, and before digging the google, i have a questions on the fly...

    suppose i run a php server on my mac, will it be possible (and how...) to send an email that is automatically translated to instructions for this "tool"?

    suppose i send an email formatted in a given way, and i say record this from this time to this time.

    What should i search for in google to achieve this?

    thanks! :)
     
  7. MacUser2525, Dec 10, 2013
    Last edited: Dec 10, 2013

    MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #7
    No clue on PHP never use it. But you could still accomplish what you want to do here with a combination of cron, a bash script and ftp or similar. The way I see it working ftp a file to your server containing the parameters you want passed to the bash script to do the recording. The cron job checks at a certain interval to see if new file has been uploaded to a directory you specify which if file is found parses it to start the recording as you specified.

    Edit: Actually now I think about it more since you can load a cronjob from a setting file import that is what the file could contain the entire cron job needed to start your recording at the time specified.
     

Share This Page