Re: Help needed with hanging bash script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 5:24 PM -0400 6/25/07, Matthew J. Roth wrote:
>Bash gurus,
>
>I have a bash script that monitors a directory for files.  Whenever it
>finds files in this directory, it passes them to a support script for
>processing.  The support script moves the files to another directory
>prior to processing them, and it is run in the background to prevent
>blocking the main script.

This is a race condition.  If for any reason the moving happens while the
ls is running your script could get very confused.  Move the files in the
main script, not asynchronously.  Also, ensure that bg_script is not
running before running it again.

A simplified version of the main script loop
>follows:
>
>  # Execute once every 10 seconds
>  while true;
>  do
>     # Fork a background script to process each file in the spool directory
>     for fname in `ls /spool/dir/*.ext 2> /dev/null`
>     do
>        bname=`basename $fname`
>
>        bg_script $bname &
>     done
>
>     sleep 10
>  done
>
>This is pretty simple and it worked flawlessly for over a year on a dual
>processor server running Fedora Core 3.  However, after upgrading to an
>8 core (2 CPUs x 4 cores) server running Fedora Core 6 the script hangs
>a few times a week.  This is a bad thing, so I have to keep a close eye
>on the server until the bug is resolved.
>
>The process tree of the script when it's hanging follows:
>
>  [root@server ~]# ps axjf
>   PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
>      1  3512  3510  2302 ?           -1 S        0   0:59 /bin/bash
>/usr/local/bin/script
>   3512 21432  3510  2302 ?           -1 R        0  40:50  \_ /bin/bash
>/usr/local/bin/script
>
>Note that the parent process (PID 3512) is sleeping and has accumulated
>relatively little CPU time since boot.  The child process (PID 21432) is
>running in a hard loop and top shows that it is consuming 100% of one of
>the cores.  It also never terminates, so it permanently blocks the
>parent process.  If the child process is killed, the execution of the
>parent process restarts without any problems.
>
>The interesting thing is that the script never calls itself.
 ...

It does.  Subshells:

    `ls /spool/dir/*.ext 2> /dev/null`
    `basename $fname`
-- 
____________________________________________________________________
TonyN.:'                       <mailto:tonynelson@xxxxxxxxxxxxxxxxx>
      '                              <http://www.georgeanelson.com/>

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux