LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help me exit my script w/o leaving an orphan process running (https://www.linuxquestions.org/questions/programming-9/help-me-exit-my-script-w-o-leaving-an-orphan-process-running-848345/)

BrokenTusk 12-04-2010 11:28 AM

Help me exit my script w/o leaving an orphan process running
 
Hello:
It's called TextRipper and it creates indexable and editable text files from any image. Feel free to copy TextRipper; it's licenced under GPL. My troubles are with cracking passworded pdf's. (As you can see, I meant any when I said any image.)
I can't find a way for the user to cancel the decryption process by exiting the script without pdfcrack orphaning. Line 349 works the rest of the time; the concerned process is line 228. I believe it's a piping/subshell problem. At least that's how I've been going at it in vain.
It's long but if you can help me you're probably going to need it all.
Please note that I've made portability a priority and that the comments still require updating.

Thanks for your help.

Code:

#!/bin/sh
# Author : dave@meyer.LA
# Date  : 10/10/2010
#
# TextRipper 3.0 (aka T-Rip)
# formerly called Text Recognition Ver. 2.0
# Tesseract engine by default!
# New: Multi-file support!
# Enhanced XSANE output and TIFF compatibility.
# New: now handles PDF's and fax's too!
# An OCR, Optical Character Recognition, gui application and cli script
# This script will convert almost any image of text (except pdf's)*1 into editable text.
#
# For OCR'ing, the preferred formats are of the pnm class: .pnm .pbm .pgm .ppm or .tif
# The most common compatible formats are: .tiff .jpg .jpeg .gif .png .bmp .xcf .pct .pict .pdf (for a full list see the first filter below)
#
# These recognition engines have a very high character recognition success rate compared to other OCR's, including proprietary software.
#
# REM: The better/cleaner/higher contrasted/higher resolution your image/scan is the better the results
#
# Dependencies: libtiff-dev (or -devel)(installed BEFORE), tesseract-2.04 (latest stable-version), your chosen language data for Tesseract (2.00 and up) *2,
# ImageMagick, ghostscript, Xdialog, and OpenOffice or other text editor *3
# This version of tesseract can be downloaded from here: http://code.google.com/p/tesseract-ocr/downloads/list
# Warning: This script will not work with the latest beta version (tesseract 3.00 pre-release) due to database structure modifications.
#
# Optional dependencies: ocrad ->an alternate recognition engine | if the inital results are unsatisfactory, maybe this engine will do better
# The latest version of ocrad can be downloaded off the GNU mirror list here: http://www.gnu.org/software/ocrad/
#
# Also: Make sure to select Unicode UTF-8 in OpenOffice's pop-up window (or text editor of your choice).
#
#
#
# *2        Install Tesseract first. Then extract all the language databases you need into the "wherever_you_installed/tesseract-2.04/tessdata" directory.
#                This is done automatically if you extract the language databases from WITHIN the "tesseract-2.04" directory (and allow overwriting).
#                This script allows the use of multiple language databases. Default is English and French. For adding others see comments below.
#                You NEED at least one language database or tesseract will not work.
# *3        Simply change the occurance of "soffice -writer" below to a text editor of your choice, ie: gedit, KWrite, etc
#                Some systems call on OpenOffice Writer differently. If unsure, check the properties tab of your Writer launcher.
#                Ie: On customized versions of OOo (such as the ones provided by Linux Mandrake or Gentoo), you start Writer with: oowriter
#
#
# Troubleshooting:
#                If this script ends saying your text editor can't open "OCR output-editable text.txt",
#                or if run off the cli: Unable to load unicharset file /usr/local/share/tessdata/eng.unicharset
#                do (as superuser):
#                echo /usr/local/share /usr/share | xargs -n 1 cp -R wherever_you_installed/tesseract-2.04/tessdata
#                        Explanation:        Tesseract may call on the tessdata directory from the /share directory of your filesystem,
#                                                        so you need to make your language databases available from there.
#

# Some preliminiaries
IFS=
unset DN_FILE
unset MC_YES

# Set variables
NUM_SEL=$#                                                                # This is to workaround $# being reset to zero
ALL_FILES=$@
DN_FILE="$( cd "$( dirname "$1" )" && pwd )"        # Highly portable current dir variable definition
export DN_FILE
i=1

# First we make sure something's selected.
if [ $# -eq 0 ]; then
Xdialog --ok-label "Oops" --title "Error" --msgbox "TextRipper won't work if you don't select at least one file...\n\n\n
        CLI fans do './TextRipper FileToRip'" 10 65
exit
fi

#initial clean up       
cd "$DN_FILE"
rm -f *converted_from_pdf*
rm -f *converted_for_ocr*
rm -f *converted_for_tes*
rm "$DN_FILE"/pdfinfo_out
echo "done"

# Ocrad installed?
OCRAD_YES=`ls /usr/local/bin /usr/bin /$HOME/bin | grep ocrad`
if [ $OCRAD_YES = ocrad ]; then
        # Prompt for multi-column or block text
        MC_YES=`Xdialog 2>&1 --item-help --title "Format definition" \
        --radiolist "Is the text in multiple columns?" 13 60 5 \
        "No"  "(skip format analysis)" on "Tesseract OCR engine"\
        "Yes"  "(inferior recognition accuracy)" off "Ocrad OCR engine"`
EXIT4=$?
        if [ $EXIT4 -ne 0 ] ; then
                exit
        fi
export MC_YES
fi

# Prompt for which language database to use
# To add other languages apphend this list then continue reading below
if [ $MC_YES = No ]; then
USE_LANG=`Xdialog 2>&1 --no-tags --item-help --title "Language selection" \
        --radiolist "Please select the language of the text in the image(s):" 13 60 5 \
        "English"  "English" on "anglais"\
        "French"  "French" off "français"\
        "Other"  "Other" unavailable "To add languages, open script in an editor and read the comments."`
EXIT3=$?
        if [ $EXIT3 -ne 0 ] ; then
                exit
        fi
fi

(while [ $# -ge 1 ] ; do
echo "10"

# Set variables to be affected by shift
FILE=$1                                                                        # This is for portability
BN_FILE="$( basename "$FILE" )"                                # Highly portable $1 variable definition
CONV_TIF="${BN_FILE} (converted_for_tes).tif"
CONV_PGM="${BN_FILE} (converted_for_ocr).pgm"
OCRd_FILE="${BN_FILE} (editable and indexable)"

#zero-pad "$i" (001 002 etc) for multiple file selection and to prevent any possible overwrite)
i=$(printf %03d $i)

# Test if the file is an image
# IS_IMAGE=`file -bi "$FILE_NAME" | grep -c image`
# May not work if the file is on the desktop, so we do it differently.
IS_IMAGE=`echo "$1" | grep -i -c -E  [.]pbm\|[.]pgm\|[.]ppm\|[.]pnm\|[.]jpg\|[.]gif\|[.]png\|[.]jpeg\|[.]bmp\|[.]tiff\|[.]tif\|[.]xcf\|[.]pct\|[.]pict\|[.]pdf\|[.]art\|[.]arw\|[.]avi\|[.]avs\|[.]cal\|[.]cgm\|[.]cin\|[.]cmy\|[.]cr2\|[.]crw\|[.]cur\|[.]dcm\|[.]dcr\|[.]dcx\|[.]dib\|[.]djv\|[.]dng\|[.]dot\|[.]dpx\|[.]emf\|[.]epd\|[.]epi\|[.]eps\|[.]eps2\|[.]eps3\|[.]epsf\|[.]epsi\|[.]ept\|[.]exr\|[.]fax\|[.]fig\|[.]fits\|[.]fpx\|[.]gpl\|[.]gra\|[.]hpg\|[.]hrz\|[.]htm\|[.]html\|[.]ico\|[.]inf\|[.]inli\|[.]jbig\|[.]jng\|[.]jp2\|[.]jpc\|[.]mat\|[.]matt\|iff\|[.]mon\|[.]mng\|[.]m2v\|[.]mpe\|[.]mpc\|[.]mpr\|[.]mrw\|[.]msl\|[.]mtv\|[.]mvg\|[.]nef\|[.]orf\|[.]otb\|[.]pal\|[.]pam\|[.]pcd\|[.]pcl\|[.]pcx\|[.]pdb\|[.]pef\|[.]pfa\|[.]pfb\|[.]pfm\|[.]pico\|[.]pix\|[.]ps2\|[.]ps3\|[.]psd\|[.]ptif\|[.]pwp\|[.]rad\|[.]raf\|[.]rgb\|[.]rla\|[.]rle\|[.]sct\|[.]sfw\|[.]sgi\|[.]sht\|[.]sid\|[.]sun\|[.]svg\|[.]tga\|[.]tim\|[.]ttf\|[.]uil\|[.]uyv\|[.]vica\|[.]viff\|[.]vif\|[.]wbm\|[.]wmf\|[.]wpg\|[.]xbm\|[.]xpm\|[.]xwd\|[.]ycb\|[.]yuv\|[.]x3f\|[.]p7`
  case $IS_IMAGE in
        0)
                Xdialog --wrap --title "Incompatible file format" --msgbox "This file does not seem to be an image. If it is, open it in GIMP then save as -> select the .pnm extension -> and try TextRipper on this new file.\n\n\n
                CLI fans do 'convert WeirdFile.??? NewFileToRip.pgm'" 0 0
                exit
                ;;
        1)
#xxx2
                # Test if the file is an image of the tiff class format (excepting pdf's).
                IS_PNM=`echo "$1" | grep -i -c -E [.]tiff\|[.]tif\|[.]pdf`
                if [ $IS_PNM -eq 0 ] ; then
echo "20"
                        # Since the file is not a .tiff image, convert it using ImageMagick
                        convert "$DN_FILE"/"$BN_FILE" "$DN_FILE"/"$CONV_TIF"
echo "40"
                fi               

                # Since the file is a .tiff image, ensure tesseract compatibility with ImageMagick
                IS_TIF=`echo "$1" | grep -i -c -E [.]tiff\|[.]tif`
                if [ $IS_TIF -eq 1 ] ; then
echo "20"                       
                        convert "$DN_FILE"/"$BN_FILE" -alpha off -type truecolor -type bilevel +compress "$DN_FILE"/"$CONV_TIF"
echo "40"
                fi
#xxx2
#xxx
                # Test if the file is a pdf document.
                IS_PDF=`echo "$1" | grep -i -c -E [.]pdf`

                if [ $IS_PDF -eq 1 ] ; then
echo "20"

                        # Test if encrypted
                        pdfinfo "$BN_FILE" > "$DN_FILE"/pdfinfo_out

                        if test -s "$DN_FILE"/pdfinfo_out ; then
                                # it is not encrypted
                                # grep total number of pages
                                TOT_PAGES=`pdfinfo "$DN_FILE"/"$BN_FILE" | awk '/Pages/ {print $2}'`

                                if [ "$TOT_PAGES" -ne 1 ] ; then
                                        # Prompt for which pages to extract
                                        PAGES=`Xdialog  2>&1 --separator " " --no-cancel --title "Page range selection" --2spinsbox "Please select the pages to extract." \
                                                360x200 1 $TOT_PAGES 1 "              to..." 1 $TOT_PAGES $TOT_PAGES ""`
                               
                                                case $? in
                                                        0)
                                                                START_PAGE=`echo $PAGES | awk '{print $1}'`
                                                                END_PAGE=`echo $PAGES | awk '{print $2}'`;;
                                                        *)
                                                                exit;;
                                                esac
                                        # Convert to ppm
                                        pdftoppm "$DN_FILE"/"$BN_FILE" -f $START_PAGE -l $END_PAGE -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "40"
                                fi

                                if [ "$TOT_PAGES" -eq 1 ] ; then
                                        # Convert to ppm
                                        pdftoppm "$DN_FILE"/"$BN_FILE" -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "40"
                                fi
               
                        else # it is encrypted
                                # Prompt for password and offer PDFcrack
                                PDF_PW=`Xdialog 2>&1 --wrap --cancel-label "Help" --password --title "${BN_FILE} is password locked" --inputbox "Enter your password. (If you forgot your password click Help for an option)" 0 0 "password"`
#window size was 15 70
                                EXIT1=$?

                        if [ $EXIT1 -eq 0 ] ; then
                                # grep total number of pages
                                TOT_PAGES=`pdfinfo -upw "$PDF_PW" "$DN_FILE"/"$BN_FILE" | awk '/Pages/ {print $2}'`

                                if [ "$TOT_PAGES" -ne 1 ] ; then
                                        # Prompt for which pages to extract
                                        PAGES=`Xdialog  2>&1 --separator " " --no-cancel --title "Page range selection" --2spinsbox "Please select the pages to extract." \
                                                360x200 1 $TOT_PAGES 1 "              to..." 1 $TOT_PAGES $TOT_PAGES ""`
                               
                                                case $? in
                                                        0)
                                                                START_PAGE=`echo $PAGES | awk '{print $1}'`
                                                                END_PAGE=`echo $PAGES | awk '{print $2}'`;;
                                                        *)
                                                                exit;;
                                                esac
                                        # Convert password-supplied pdf to pgm
                                        pdftoppm -upw "$PDF_PW" "$DN_FILE"/"$BN_FILE" -f $START_PAGE -l $END_PAGE -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "40"
                                fi

                                if [ "$TOT_PAGES" -eq 1 ] ; then
                                        # Convert password-supplied pdf to pgm
                                        pdftoppm -upw "$PDF_PW" "$DN_FILE"/"$BN_FILE" -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "40"
                                fi
                        fi

                        if [ $EXIT1 -ne 0 ] ; then
                                #offer PDFcrack
                                Xdialog --wrap --ok-label "Next" --cancel-label "Exit TextRipper" --title "Special option" --yesno "Would you like TextRipper to crack your forgotten password?\n\n
                                Although this can take DAYS depending on the password length and processor speed and will stress your system,\n
                                TextRipper will work in the background and you may suspend and resume your computer.\n
                                TextRipper will auto-close once the process is terminated and the text extracted.\n\n\
                                You can interupt decryption at any moment by closing the progress bar." 0 0 #window size was 20 110
                                EXIT2=$?       
                        fi
#chg = to -eq but had to chg it back       
                if [ x"$EXIT2" = x0 ] ; then #quoted the variable and added a prefix to rid an "integer expression" or "unary operator" expected error if $EXIT2 is unset

                        PW_LEN=`Xdialog 2>&1 --wrap --cancel-label "Exit TextRipper" --title "Stop trying when reaching this length" --rangebox "To timeout the process before you reach old age, estimate the maximum length of the password.\n
                        (If the actual password is longer than your estimate, TextRipper, after having tried all passwords up to this length, will fail and exit.\n\n
                        Note: Although pdf passwords can be up to 32 characters long, on a PC, 8-character passwords take about 500 years to crack...\n
                        Hence the 7-character limit ;)\n\
                        TextRipper does allow for these special characters: @#&'()*-_?,.;:+=%$. " 0 0 1 7 4` #window size was 20 110
                                case $? in
                                          0)
                                            PDF_CPW=`pdfcrack -q --maxpw=$PW_LEN --charset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 @#&'()*-_?,.;:+=%$" -f "$DN_FILE"/"$BN_FILE" | sed -e "s/'//g" -e 's/found //g' -e 's/user-password: //g' -e 's/owner-password: //g'`;;
                                          *)
                                            exit;;
                                esac
                        else
                                exit
                fi
                               
                                # grep total number of pages
                                TOT_PAGES=`pdfinfo -upw "$PDF_CPW" "$DN_FILE"/"$BN_FILE" | awk '/Pages/ {print $2}'`

                                if [ "$TOT_PAGES" -ne 1 ] ; then
                                        # Prompt for which pages to extract
                                        PAGES=`Xdialog  2>&1 --separator " " --no-cancel --title "Page range selection" --2spinsbox "Please select the pages to extract." \
                                                360x200 1 $TOT_PAGES 1 "              to..." 1 $TOT_PAGES $TOT_PAGES ""`
                               
                                                case $? in
                                                        0)
                                                                START_PAGE=`echo $PAGES | awk '{print $1}'`
                                                                END_PAGE=`echo $PAGES | awk '{print $2}'`;;
                                                        *)
                                                                exit;;
                                                esac
                                        # Convert cracked pdf to ppm
                                        pdftoppm -upw "$PDF_CPW" "$DN_FILE"/"$BN_FILE" -f $START_PAGE -l $END_PAGE -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "60"
                                fi

                                if [ "$TOT_PAGES" -eq 1 ] ; then
                                        # Convert cracked pdf to ppm
                                        pdftoppm -upw "$PDF_CPW" "$DN_FILE"/"$BN_FILE" -gray -r 600 "$DN_FILE"/"${BN_FILE} (converted_from_pdf)"
echo "60"
                                fi
                        fi
                fi
                                # OCR operation
                                if [ $MC_YES = No ] ; then
                                        # Convert pgm's to tif (w/o the +adjoin option results in one tif file)                       
                                        cd "$DN_FILE"
                                        convert *converted_from_pdf* "$CONV_TIF"
echo "70"
                                                case $USE_LANG in
                                                        English)
                                                                # The following works on the Desktop too!
                                                                tesseract "$DN_FILE"/"$CONV_TIF" "$DN_FILE"/"${OCRd_FILE} $i" -l eng
                                                                ;;
                                                        French)
                                                                # The following works on the Desktop too!
                                                                tesseract "$DN_FILE"/"$CONV_TIF" "$DN_FILE"/"${OCRd_FILE} $i" -l fra
                                                                ;;
                                                        # Uncomment to add other language(s). Replace variables "Other_Language" and "Other_Language_ID".                       
                                                        # Other_Language)
                                                                # # The following works on the Desktop too!
                                                                # tesseract "$DN_FILE"/"$CONV_TIF" "$DN_FILE"/"${OCRd_FILE} $i" -l Other_Language_ID
                                                                # ;;
                                                esac
echo "80"
                                fi

                                if [ $MC_YES = Yes ] ; then
                                        cd "$DN_FILE"
                                        convert "$CONV_TIF" "$CONV_PGM"
echo "70"
                                        for PGMs in *converted_from_pdf* ; do
                                        ocrad -a -l --format=utf8 "$DN_FILE"/"$PGMs" -o "$DN_FILE"/"${OCRd_FILE} $i.txt" #results in one file
                                        done
                                        ocrad -a -l --format=utf8 "$DN_FILE"/"$CONV_PGM" -o "$DN_FILE"/"${OCRd_FILE} $i.txt"
echo "80"
                                fi               
#xxx
                ;;
        *)
                Xdialog --wrap --title "Corrupted file" --msgbox "Open file in GIMP then save as -> select the .pnm extension -> and try TextRipper on this new file.\n
                Otherwise rescan the page.\n\n\n
                CLI fans try 'convert WeirdFile.??? NewFileToRip.pgm'" 0 0               
                exit
                ;;
  esac

# Increment "$i" for the next file
let i++

# And continue with the next file ...
  shift
done

#clean up       
cd "$DN_FILE"
rm -f *converted_from_pdf*
rm -f *converted_for_ocr*
rm -f *converted_for_tes*
rm -f pdfinfo_out
echo "100"

if [ $NUM_SEL -eq 1 ] ; then
        # Open the newly created output file (change "soffice -writer" to text editor of your choice)
        soffice -writer "$DN_FILE"/"${OCRd_FILE} 001.txt"
fi
echo "101" #close progress bar
) | Xdialog --wrap --title "Rippin text" --gauge "TextRipper is processing... $ALL_FILES\n\n\n
If the result is one output file, TextRipper will open it for you in OpenOffice Writer.\n\n
Otherwise all txt output files (editable and indexable) will be in the original file's directory." 0 0

if [ "$?" = 255 ] ; then
        break
fi

unset DN_FILE
unset MC_YES


grail 12-04-2010 09:33 PM

So the first problem I have is that the script only has 347 lines each time I copy it and 228 puts me on a blank line :(

Would you be able to tell / highlight the code parts you are having issue with?

BrokenTusk 12-05-2010 05:32 PM

precision
 
Grail:
thanks for looking into this.
The process being orphaned if the script is exited:
Code:

PDF_CPW=`pdfcrack -q --maxpw=$PW_LEN --charset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 @#&'()*-_?,.;:+=%$" -f "$DN_FILE"/"$BN_FILE" | sed -e "s/'//g" -e 's/found //g' -e 's/user-password: //g' -e 's/owner-password: //g'`;;
And the global progress window that should allow a script exit at anytime (although another means would be fine too):
Code:

) | Xdialog --wrap --title "Rippin text" --gauge "TextRipper is processing... $ALL_FILES\n\n\n
If the result is one output file, TextRipper will open it for you in OpenOffice Writer.\n\n
Otherwise all txt output files (editable and indexable) will be in the original file's directory." 0 0

if [ "$?" = 255 ] ; then
        exit
fi

I'm sort of an autodidact. This is the first time I ask a question in a forum, which also makes it the first time I'm getting a reply. So thanks again.
dave

grail 12-05-2010 06:35 PM

Well I agree that it appears to be some kind of subshell issue (seeing there are many within the code).

Just to confirm, you are saying that all other tasks that can be done within the main subshell (handled by Xdialog wrapper above) are able to be terminated
successfully when the cancel option is chosen?

I ask as I am not sure that closing the subshell would by default close anything running within it, but is more likely to orphan the process ( as you have found )
until it has completed or is killed.

BrokenTusk 12-06-2010 06:18 AM

The thing is, pdfcrack is the only process that works for such a long time. Ie: convert will just do its thing or exit with error and that's all, so if you hit cancel even if it gets orphaned it's just a matter of seconds. But pdfcrack, unless it is killed, which i can't successfully code, just keeps on going and this can be a matter of days.

grail 12-06-2010 06:26 AM

So then the real issue is finding a way to best kill this orphaned item.
Are there no related PIDs? I would have thought the script PID would be the parent or grandparent of this line of code??
So if you sift through ps you should be able to come up with an easy way to kill it or maybe even trap the cancel to kill it.

BrokenTusk 12-06-2010 12:50 PM

Okay I think now were getting somewhere. I can't trap and I can't kill pid of because the script waits for pdfcrack to terminate before executing the next command. At this point, it seems that the logical solution would be to background the process, but i can't get this to work because wait doesn't do its thing. IOW if I bg pdfcrack, the script then just continues without waiting for the cracked password.

grail 12-06-2010 06:00 PM

Quote:

I can't trap and I can't kill pid of because the script waits for pdfcrack to terminate
Are we not missing the point here?? Yes the normal flow of the program is to run sequentially and when pdfcrack normally terminates then
the program continues unabated. Our issue is that we ourselves have terminated the script as a whole by clicking / selecting cancel which we know will still
allow pdfcrack run until completion but we would like it to all stop at the same time.

Therefore, add to the script that only when selecting cancel that you check prior to completion of main script for any other child PID related to it and then kill them
as they should not be running once script has completed.

BrokenTusk 12-06-2010 06:39 PM

Quote:

Originally Posted by grail (Post 4182853)
Are we not missing the point here?? Yes the normal flow of the program is to run sequentially and when pdfcrack normally terminates then
the program continues unabated. Our issue is that we ourselves have terminated the script as a whole by clicking / selecting cancel which we know will still
allow pdfcrack run until completion but we would like it to all stop at the same time.

This is right on.
Quote:

Therefore, add to the script that only when selecting cancel that you check prior to completion of main script for any other child PID related to it and then kill them
as they should not be running once script has completed.
This I only partially understand, especially: "only when selecting cancel that you check prior to completion of main script."
Could you give me a simple working example, or perhaps an example applied to the T-Rip script?"
Thanks again, Grail.

grail 12-06-2010 07:03 PM

Well I am not overly familiar with Xdialog, but I am guessing that you have some buttons available to you via this mechanism, one of
which is a cancel button (which I gleaned from where you wrote 'And the global progress window that should allow a script exit at anytime')

I would also surmise that depending on which buttons are pressed that Xdialog alters its exit status (or maybe you can even program the exit status depending
on the button selected). So where you have:
Code:

if [ "$?" = 255 ] ; then
        break
fi

Add a new if (or maybe even a case statement if there are several options) to allow for cancel being selected and then use awk / sed / grep to get the correct PID
for pdfcrack and kill that sucker :)

Also, if I may suggest, when interacting with numbers you should use the appropriate tests as opposed to forcing it to be a string comparison.
Code:

# if sh is only option then
if [ $? -eq 255 ] ; then

# if bash is an option
if (( $? == 255 )); then

Hope this helps.

BrokenTusk 12-07-2010 04:42 AM

Quote:

Originally Posted by grail (Post 4182911)
Well I am not overly familiar with Xdialog, but I am guessing that you have some buttons available to you via this mechanism, one of
which is a cancel button (which I gleaned from where you wrote 'And the global progress window that should allow a script exit at anytime')

Add a new if (or maybe even a case statement if there are several options) to allow for cancel being selected and then use awk / sed / grep to get the correct PID
for pdfcrack and kill that sucker :)

Thank you for your string comparison suggestion. I learned something. About telling Xdialog what to do when clicking cancel or the window close button, that's clear and works fine IF the console is accepting commands. But my whole problem is that with pdfcrack running the console waits for it to terminate before executing the (user intervention) cancel command. (Thereby not really canceling.)

Remember that I wrote: "... pdfcrack is the only process that works for such a long time. Ie: convert will just do its thing or exit with error and that's all, so if you hit cancel even if it gets orphaned it's just a matter of seconds. But pdfcrack, unless it is killed, which i can't successfully code, just keeps on going and this can be a matter of days."

grail 12-07-2010 06:13 AM

Ok ... I think I am following. So if I were to have the Xdialog screen in front of me and press the cancel button I will still wait for pdfcrack to complete ... correct?

Assuming this is the case, maybe you should place it in the background and continually check if its pid is still running and only continue when completed.
This way you could also build in the cancel option so that the loop checking the pid would then also check either for the pid of the main program is still
running or that the cancel button has been pressed.

What do you think?

Pseudo code:

while cancel button has not been pressed and pid of pdfcrack is still available wait for ~30 seconds (time is up to you of course)

ntubski 12-07-2010 09:03 AM

I would suggest using pkill:
Code:

pkill -g $$
That will kill all processes with the same pgid as the current script, which will include all the processes spawned from it. You can use that as the trap SIGINT action or run it after the user clicks cancel.

BrokenTusk 12-07-2010 07:08 PM

Okay Grail I'm going to try to apply your input as soon as I find the time. I agree with your reasoning, let's see if I can make it work.
Ntubsky, pkill -g $$ is a definite exit but if the script hangs while waiting for a subshell to terminate I can't call it. It is this work around that I'm looking for.

ntubski 12-07-2010 09:15 PM

Quote:

Originally Posted by BrokenTusk (Post 4184139)
Ntubsky, pkill -g $$ is a definite exit but if the script hangs while waiting for a subshell to terminate I can't call it. It is this work around that I'm looking for.

Okay, got confused by the term orphan process.

Grail's suggestion will probably work, but I'll suggest a non busy wait solution. Here's an abstracted example (I use zenity because Debian dropped Xdialog):

Code:

#!/bin/sh

trap 'rm -f pipe.$$' EXIT INT TERM QUIT # make sure we clean up
mkfifo pipe.$$

(
    OUTPUT=`sleep 200; echo done` # process that takes a long time

    # do stuff with OUTPUT
    # ...

    # tell zenity we're done
    echo 100 >&3

) 3>pipe.$$ &    # redirect is necessary to open the pipe for writing

PID=$!

if zenity --progress --text='Please wait...' < pipe.$$ ; then
    # user pressed Ok, we finished
    echo Yay
else
    echo ABORT!!
    kill $PID
fi

The fifo can be probably be avoided with bash-v4+ coprocesses.


All times are GMT -5. The time now is 02:53 AM.