Gentoo Archives: gentoo-user

From: Heinz Sporn <heinz.sporn@××××××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Script Run From Cron Sometimes Leaves Defunct sh Process
Date: Mon, 24 Oct 2005 10:53:20
Message-Id: 1130141053.10170.13.camel@spok.local.sporn-it.com
In Reply to: [gentoo-user] Script Run From Cron Sometimes Leaves Defunct sh Process by Drew Tomlinson
1 Am Sonntag, den 23.10.2005, 07:36 -0700 schrieb Drew Tomlinson:
2 > I wrote a script to read a smbfs mounted filesystem and make symlinks to
3 > the files locally. The script appears to run fine interactively and
4 > fine most of the time when run by cron. I run the script every half
5 > hour. Over the course of a week or so, 15 - 20 defunct sh processes
6 > show up in the ps output. I've Googled and learned that the "child" (my
7 > script) is exiting but the "parent" (cron) is still around. So now my
8 > question is "why" and what's wrong with my script to cause this
9 > occasional behavior. And more importantly, how can I fix it? :)
10 >
11 > Thanks for your help!
12 >
13 > Drew
14 >
15 > --- BEGIN ---
16 > #! /bin/sh
17 >
18 > # 10/13/05
19 > # This script creates symlinks to media files on Blacklamb. Because MythTV
20 > # needs to write it's own "." files and such, mounting a read-only share
21 > and
22 > # then creating symlinks locally keeps the share on Blacklamb clean.
23 >
24 > remote_dir="/multimedia/Pictures"
25 > local_dir="/tv/pictures"
26 > find_args="-iname \"*.jpg\" -or -iname \"*.gif\""
27 >
28 > # Don't wipe out the entire directory or else MythTV has to recreate all its
29 > # local cache files. Consider using find with above $find_args to remove
30 > # symlinks and avoid listing each *.xxx explicitly.
31 > echo -e "\nRemoving old symlinks from $local_pictures_dir..."
32 > rm "$local_pictures_dir"/*.[Jj][Pp][Gg]
33 > rm "$local_pictures_dir"/*.[Gg][Ii][Ff]
34 >
35 > # Default sh delimiter is a "space" and is stored in $IFS.
36 > # This caused $original to be truncated at the first "space" in the file
37 > name.
38 > # Save $IFS and then set sh delimeter to a newline so 'for' loop works.
39 > OLDIFS=$IFS
40 > IFS='
41 > '
42 > echo -e "\nCreating new symlinks in $local_pictures_dir..."
43 >
44 > # Search directory contained in $remote_dir using criteria in $find_args
45 > for original in \
46 > $( eval "/usr/bin/find $remote_dir \( $find_args \) -print" )
47 > do
48 >
49 > # Remove $remote_dir from filename and replace remaining "/"
50 > with "-"
51 > # and "spaces" with "_". Save in $newfile
52 > newfile=`echo $original | cut --delimiter="/" --fields=4- | \
53 > sed -e s/"\/"/-/g -e s/" "/"_"/g`
54 >
55 > # Create symlink from original file to $newfile in $local_dir
56 > # specified above.
57 > ln -s "$original" "$local_dir/$newfile"
58
59 Just a suggestion: insert a minimum of error handling like
60
61 ln -s "$original" "$local_dir/$newfile"
62 if (( $? > 0 )) ; then logger -t "myscript" "That didn't work for some
63 reason ; fi
64
65 ... or something else at points where the script could fail.
66
67 >
68 > # Increase count by 1
69 > count=$(( count + 1 ))
70 > done
71 >
72 > # Reset $IFS to original value
73 > IFS=$OLDIFS
74 >
75 > # Print number of symlinks created.
76 > echo -e "\nCreated $count symlinks."
77 >
78 > exit
79 > --- END ---
80 --
81 Mit freundlichen Grüßen
82
83 Heinz Sporn
84
85 SPORN it-freelancing
86
87 Mobile: ++43 (0)699 / 127 827 07
88 Email: heinz.sporn@××××××××.com
89 heinz.sporn@××××××.at
90 Website: http://www.sporn-it.com
91 Snail: Steyrer Str. 20
92 A-4540 Bad Hall
93 Austria / Europe
94
95 --
96 gentoo-user@g.o mailing list

Replies

Subject Author
Re: [gentoo-user] Script Run From Cron Sometimes Leaves Defunct sh Process Drew Tomlinson <drew@××××××××××××××.net>