Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ps command

Status
Not open for further replies.

Yarka

Technical User
Jan 14, 2007
192
ES
hi,
i run a script that it read a file and extract data:

eafs:~/root #./script.sh input.txt

When I want to see if my script is started I do:

eafs:~/root #ps -ef | grep script.sh
and my output is:
root 10805 1 2 16:35 ? 00:01:42 /bin/sh ./script.sh input.txt
root 3629 1 0 16:52 ? 00:00:00 /bin/sh ./script.sh input.txt
root 3644 3629 2 16:52 ? 00:01:25 /bin/sh ./script.sh input.txt
root 22149 27917 0 17:14 pts/8 00:00:00 /bin/sh ./script.sh input.txt
root 22157 22149 2 17:14 pts/8 00:00:46 /bin/sh ./script.sh input.txt
root 5546 10805 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5548 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5549 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5553 3644 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt

thanks.
 
Based on your output, I am assuming that you want to know why it shows up so many times.

Without seeing the script, I would guess that maybe one/all of the following:

1.) You didnt get any response, so you started it again
2.) The script is trying to access something that is not ready/available
3.) The script is somehow calling itself

If you can post the script, it would be easier for someone to figure out why its showing up multiple times.
 
my scripts is large because I have do a lot of checks, but it is as:

cat $1 | while read LINE; do

if..
else..
fi

until .. do
if..
else..
fi
read LINE
if ..
read LINE
else
fi
..
done
...
until...
done
...
done

Now, I suppose that it is fot read LINE, but it isn't correct can put read LINE inside cat $1 | while read LINE; do? I put it because i need to do more checks to the next lines !!
and is it because it takes a long time for execute?

 
instead of

Code:
cat $1 | while read LINE; do

try it like this:

Code:
while read -r LINE
do

<rest of your script here>

done < /path/to/$1

Base on the "framework" that you posted, it appears that your script is getting hung on the initial "cat $1..." line.
 
As a little piece of my script is worked bad:

while read -r LINE
do
m=`echo "$LINE" | awk -F'\t' '{ print $5 }'`
l=${m:1:1}
if [ $l == ":" ]; then
m=${m:2:2}
else m=${m:3:2}
fi
done < /root/$1

my input is as:
1 A 123 10 1:33:25,932326000 00,000000000 34.00B
2 B 234 10 15:33:25,938999000 00,006673000 ww.ll
3 A 890 10 15:33:25,950477000 00,011478000 43.00B
4 A 11 10 15:33:25,950662000 00,000185000 43.00B
5 C 22 10 15:33:25,951127000 00,000465000 99.00B

when I do:
eafs:~/root #ps -ef | grep script.sh
it sometimes has 1 or 2 or3 processes !!
Why?
 
I can't see anything wrong with your scripts, and don't see why they should be hanging and staying in ps like that. I tested doing a while read LINE ; do ... ; done < input.txt loop with additional read LINE statements inside the loop and it seems to work fine for me.

As sbrews said, we probably need to see more of your script to understand why it's failing.

With the most recent script you posted, why do you say it "worked bad"? What output should it produce for the input you provided?

Annihilannic.
 
Incidentally, in your position I would use awk to process the file rather than shell script, since that is what awk is designed for. Or perl if you prefer it.

Annihilannic.
 
When I do:
eafs:~/root #ps -ef | grep script.sh
It changes to 1 or 2 or 3 processes. I think that it only would have 1 process running and it doesn't change to 2 and 3. The input file is large, for if it can help.
 
That is quite normal, it will often need to spawn 'child' processes.

You should see that the parent process of some of the processes is the original script. For example, in your original post, you can see that 10805 spawned 5546, and 5546 spawned 5548 and 5549 by checking the PPID (parent process ID) column:

[tt]UID PID PPID
root 10805 1 2 16:35 ? 00:01:42 /bin/sh ./script.sh input.txt
root 5546 10805 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5548 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5549 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt[/tt]

Annihilannic.
 
ok, i don't know it. thanks Annihilannic
 
But is my output is as my original post, i.e:

eafs:~/root #ps -ef | grep script.sh
and my output is:
root 10805 1 2 16:35 ? 00:01:42 /bin/sh ./script.sh input.txt
root 3629 1 0 16:52 ? 00:00:00 /bin/sh ./script.sh input.txt
root 3644 3629 2 16:52 ? 00:01:25 /bin/sh ./script.sh input.txt
root 22149 27917 0 17:14 pts/8 00:00:00 /bin/sh ./script.sh input.txt
root 22157 22149 2 17:14 pts/8 00:00:46 /bin/sh ./script.sh input.txt
root 5546 10805 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5548 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5549 5546 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt
root 5553 3644 0 17:43 ? 00:00:00 /bin/sh ./script.sh input.txt

Does it mean that I have wrong my sctipt?
 
It looks like you have at least 3 copies of your script running, PIDs 10805, 3629 and 22149. You should probably kill them all and just start 1 copy.

Annihilannic.
 
I have killed them, but when I run my script and I check it with ps -ef command I have the same result.
 
You mustn't have killed them successfully. Did you check whether they were gone when you killed them? If they didn't die, did you try kill -9?

Annihilannic.
 
I have killed them with kill -9, but I have the same.
 
Something bad is happening then. What is the exact kill command you used? What is the output of pstree -capG 10805?

You might have to reboot the system to clear them if nothing else works.

Annihilannic.
 
I use kill -9 pid

Now when I run my script I have:
ps -ef | grep a.sh
root 31951 25861 0 18:46 pts/7 00:00:00 /bin/sh ./a.sh c.txt
root 31953 31951 10 18:46 pts/7 00:01:45 /bin/sh ./a.sh c.txt
root 31904 13902 0 19:03 pts/2 00:00:00 grep a.sh
root 31910 31953 0 19:03 pts/7 00:00:00 /bin/sh ./a.sh c.txt
root 31911 31910 0 19:03 pts/7 00:00:00 [a.sh] <defunct>
root 31912 31910 0 19:03 pts/7 00:00:00 /bin/sh ./a.sh c.txt
root 31913 31910 0 19:03 pts/7 00:00:00 /bin/sh ./a.sh c.txt

When I do: pstree -capG 31951, my output is
a.sh,31951 ./a.sh c.txt
??a.sh,31953 ./a.sh c.txt
? ??a,17906 ./a.sh c.txt
? ??awk,17908 -F\t {\040print\040$5\040}
??cat,31952 c.txt

What it mean?



 
It looks like it's working away to me. Does it ever complete?

You will probably find it is quite slow; like I said previously, you should probably use awk or perl for this kind of work; it is what they are designed for, and they would not need to spawn child processes the way your shell script does which means they would run much faster.

If you can show us a sample of your input data and the kind of checks you want to perform on it, we can give you an example of how to do it in one of the other languages.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top