Find the answer to your Linux question:
Results 1 to 7 of 7
Hi! I have a crontab that runs sample.sh every 1 minute that converts pictures. What if the conversion isn't finish yet within 1 minute and again the sample.sh script runs ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2006
    Posts
    12

    Smile how to write a script to prevent the process to overlap?


    Hi!

    I have a crontab that runs sample.sh every 1 minute that converts pictures. What if the conversion isn't finish yet within 1 minute and again the sample.sh script runs again? What will happen to the first process? Will it be completed? Or will it not complete the job?
    In line with this, I want to create a script inside the sample.sh to prevent the process from overlapping.

    I only have this
    Code:
    ps -ax | grep sample.sh
    for now. And I don't know where/how to compare it with...

    Please help...

    Thanks

  2. #2
    Just Joined!
    Join Date
    Feb 2006
    Posts
    15
    Each new process should run to completion.
    Why dont you want the processes to overlap?
    What do you want to happen to the partially converted pic?

  3. #3
    Just Joined!
    Join Date
    Jan 2006
    Posts
    12

    Question

    Quote Originally Posted by CanyonMan
    Each new process should run to completion.
    Why dont you want the processes to overlap?
    What do you want to happen to the partially converted pic?
    Actually I don't want the process to overlap. I'm worried what will happen if my process runs every minute because the conversion does not take just 1 minute if there are many pictures to convert.
    So if it takes for example, 2 minutes to convert the pictures and the script runs again, what will happen to the first conversion?

    I don't really know how a process work(ie running a script every minute). But I am thinking that each process has a unique PID(?) and therefore, it will not overlap with each other??

  4. #4
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    I think you have some confusion.

    In one way you may be right. Imagine your script looks for (example only) *.pdf files, finds 20, and has finished 10 of them at the end of one minute.
    The new process will se ten files and try to work on them while the first process is also working on them. Is this your concern?

    This is called a concurrency problem.

    Now imagine that the second process cannot find those files. Why? because you changed the script that runs every minute to move the files to a place where only the script knows where they live. Nobody else can find them:

    Code:
    #!/bin/ksh
    # place the files in temporary storage
    # $$ = pid of this process
    
    mkdir /tmp/$$
    export MYDIR=/tmp/$$
    
    # find all the file to convert for now
    
    find /path/to/files -name \*.pdf | \
    while read file
    do
        bname=`basename $file`
        mv $file $MYDIR/"$bname"  # move file to tmp directory
    done
    
    # all the files are now tucked away 
    # where only this process knows about them
    
    this_dir=`pwd`  # remember where we are now
    cd $MYDIR
    for file in `ls -1`
    do
        # put your convert file commands here eg.,:
        pdfconv "$file" -o [ /path/to/converted/files/<filename> ]
        # if pdfconv (or whatever) doesn't remove the file, let's do it now
        rm -f "$file"
    done
    cd /tmp
    rm -f -R "$$"  # remove the temporary subdirectory
    cd $this_dir
    #  .. continue exit processing here
    exit 0

  5. #5
    Just Joined!
    Join Date
    Feb 2006
    Posts
    15
    I think Jim has done a great job answering the original question but it makes more sense to write a script that converts all the pics to completion then you do not have a synchronization issue. Execute the script in the background (with the & suffix) through one of the shells and it will fork a process that will run concurrently with the other processes on your system until the pics have been converted. That is just my opinion.

  6. #6
    Just Joined!
    Join Date
    Jan 2006
    Posts
    12
    yes, i'm really confuse!


    here is my script:

    Code:
    #=======================================
    #!/bin/sh
    
    E_NOSUCHPROCESS=67
    
    SOURCE_HOST="/usr/local/tomcat/webapps/a/img/"
    DESTINATION_HOST="/usr/local/tomcat/webapps/b/img_copy/"
    
    LOGFILE=/usr/local/tomcat/webapps/b/logs/rsync.log
    
    echo pidno=$( ps ax | grep rsync.sh | awk '{ print $1 }' )
    
    if [ -z "$pidno" ]  # If, after all the filtering, the result is a zero-length string,
    then                # no running process corresponds to the pid given.
      echo "No such process running."
      
      echo "=================START======================" >$LOGFILE
    	
    	date >> $LOGFILE
    	
    	rsync --recursive --times --perms --links -avz --delete --include "*.jpg" --include "*.jpeg" --include "*.JPG" --include "*.JPEG" --include "*.png" --include "*.gif" --exclude "*.*" $SOURCE_HOST $DESTINATION_HOST  >>$LOGFILE
    	
    	#rsync --recursive --times --perms --links -avz --delete --include "*.jpg" --include "*.png" --include "*.gif" --exclude "*.*" $SOURCE_HOST2 $DESTINATION_HOST  >>$LOGFILE
    	
    	echo "==================END=======================" >>$LOGFILE
    	
    	sh /usr/local/tomcat/webapps/b/shellscript/convertpic.sh
    	#=======================================
    
      exit $E_NOSUCHPROCESS
    fi
    convertpic.sh -->this script calls the java code that converts the pictures
    Code:
    #!/bin/sh
    
    TOMCAT_HOME=/usr/local/tomcat
    JAVA_HOME=/usr/java/j2sdk1.4.2_09
    
    JAR_PATH=$TOMCAT_HOME/webapps/b/WEB-INF/lib
    WEB_INF_PATH=$TOMCAT_HOME/webapps/b/WEB-INF/classes
    PROPERTIES_PATH=$TOMCAT_HOME/bin/properties/Abc.properties:
    
    CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar:$WEB_INF_PATH:$JAR_PATH/D.jar:$JAR_PATH/E.jar:$PROPERTIES_PATH
    
    export JAVA_HOME CLASSPATH
    
    $JAVA_HOME/bin/java -Djava.library.path=/usr/local/lib connect.framer.ConvertPicture "/usr/local/tomcat/webapps/b/WEB-INF/classes/properties"

  7. #7
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    The solution is quite simple: you create a temporary file while the script is executed, wich will be deleted at the end of the script.

    Before creating the temporary file, you check the file existence: if it exists it means that the script is yet executed, so you can wait for a moment...

    Code:
    while [ -e /tmp/notoverlap ] ; do
      sleep 1;
    done
    
    touch /tmp/notoverlap
    
    ... # do your job
    
    rm -f /tmp/notoverlap
    In this way, the solution is correct: infact a script executed takes too time, will block the same second script, wich will wait until the file specified doesn't exists (so the previous script has finished).
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •