Menu

Example help

chudak
2016-05-22
2016-05-22
  • chudak

    chudak - 2016-05-22

    Hello and thank you for your FLoM program !

    I have a cron_wrapper bash script (see below) that appends to a log file and in theory crontab entries may run at the same time.

    Would you be so kind and suggest an example for this case?

    Thanks!

    ================

    !/bin/bash

    /nightlies/cron_wrapper.sh

    check for no argument case and stop

    if [ -z $1 ]; then
    echo "need argument"
    exit 1
    fi

    set permanent $LOG file var

    LOG="/var/log/crontab-nightlies-log/crontab.log"

    temp files to store sdtout and stderr

    named with the PID of this script in their name so they'll be unique

    STDERR="/var/tmp/stderr.$$"
    STDOUT="/var/tmp/stdout.$$"

    $STDOUT and $STDERR are removed when the script exits for any reason

    trap "rm -f $STDOUT $STDERR" 0

    echo "Running command: $@" >> $LOG

    run a command from this script's argument

    redirect stdout to $STDOUT file and redirect stderr to $STDERR file

    "$@" > $STDOUT 2> $STDERR

    get return code from the command run`

    code=$?

    if [ $code != 0 ] ; then
    # echoing to stdout/stderr makes cron send email
    echo "stdout:"
    cat $STDOUT
    echo "stderr:"
    cat $STDERR
    else
    # normal exit: just log stdout
    DATE=$(date)
    echo -n "$DATE: " >> $LOG
    cat $STDOUT >> $LOG
    fi

     
  • Christian Ferrari

    Dear Yuri,
    from my understanding of your script, you want to synchronize the access to the $LOG file.
    The resource is accessed by 3 different operations:

    echo "Running command: $@" >> $LOG
    [...]
            commands with unpredictable duration
    [...]
    echo -n "$DATE: " >> $LOG
    cat $STDOUT >> $LOG
    

    interleaved by some commands with unpredictable duration.
    If your intent is just to avoid overlapped log, your script could be changed with a further tmp file:

    LOG2=$(mktemp)
    [...]
    echo "Running command: $@" >> $LOG2
    [...]
    echo -n "$DATE: " >> $LOG2
    cat $STDOUT >> $LOG2
    flom -r $LOG -- bash -c "cat $LOG2 >> $LOG"
    rm $LOG2
    

    the clue is

    flom -r $LOG -- bash -c "cat $LOG2 >> $LOG"
    

    it synchronizes the execution of the command "cat ..." with an exclusive lock related to the hierarchical resource $LOG.

    You can't use:

    flom -r $LOG -- cat $LOG2 >> $LOG
    

    because the synchronized command needs a shell environment for ">>" operator.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.