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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PID of a particular process...

Status
Not open for further replies.

nithin97

Programmer
Oct 23, 2002
25
US
Hi,
I've a C Shell script inside which I do a ftp to a site. Is there any way of taking the PID of that ftp process alone 'coz when I tried to take the PID, I could just get the PID of this script alone. I used $$ to take the PID. Thanks in advance.
 
hi ecasadella,
thanks for ur reply... but i still couldnt get the pid of ftp process alone... heres the ftp process which comes inside my script.

echo ftp -inv \<\< ! > ./tmp5
echo open >> ./tmp5
echo user uname pwd >> ./tmp5
echo cd /tmp/tmp1 >> ./tmp5
echo get file1 >> ./tmp5
echo quit >> ./tmp5
chmod 744 ./tmp5
./tmp5 > ./tmp66

and to get PID I use,

echo $$ >! ./myftppid and put it in a file....

this above line I put it above the ftp process.. but it just gets the PID of this whole script rather than that ftp process alone...

now how should i use the ptree... ur help appreciated..
 
once you are in the ftp session, try this:

ftp> !ps | grep ftp

or additionally

ftp> !ps | grep ftp | awk '{print $1}' > myftppid

EDC

 
still no joy ecasadella... its not working. its giving error.
ps: Event not found

I added this line after I open the ftp process,

!ps | grep ftp | awk '{print $1}' > myftppid

Am new to shell scripting. Can yu pls help me out in this.

 
Tell me what OS are you using?

besides try this:

ftp -inv << EOF > ./log
user uname pwd
!ps | grep ftp > ./myftppid
cd /tmp/tmp1
get file1
quit

then treat myftppid as you wish

EDC
 
hi, I tried what you told but still same error.

and my OS is SunOS 5.8

When I tried the same line given below in the command prompt it worked fine...

!ps | grep ftp >! myftppid

 
here it is.. but i think something is wrong.. can u pls let me know..

echo ftp -inv \<\< ! > ./tmp5
echo open >> ./tmp5
echo user uname pwd >> ./tmp5

echo !ps | grep ftp | awk '{print $1}' >! ./myftppid

echo cd /tmp/tmp1 >> ./tmp5
echo get file1 >> ./tmp5
echo quit >> ./tmp5
chmod 744 ./tmp5
./tmp5 > ./tmp66
 
With this example you just execute the code in your script, and every message will be logged into tmp66
Try to replace those lines with these:

#!/usr/bin/ksh
FTP_USER=user
FTP_PASS=password

ftp -inv << EOF > ./tmp66
user $FTP_USER $PASS
!ps | grep ftp > ./myftppid
cd /tmp/tmp1
get file1
quit

please avoid to redirect >! , is ther a reason why you are doing this ?

EDC
 
hi ecasdella, ur example script works fine when run.. but it is a K-Shell.. i've to use C-Shell.. i think thats why that '!ps' is not working in c-shell... is it the reason..can u pls help me get out of this problem...

 
ok, to do this you can put this code in a separate script, so that whenever you need to execute it from your c-shell just add this line in your csh script:

ksh ftp_script.ksh

and if you need to pass user and password to the script, do it as common script args like:

ksh ftp_script.ksh $user $pass $host

so in the ksh script you must set them as:

USER=$1
PASS=$2
HOST=$3

once this script finish its execution the c-shell script will keep parsing its own lines.


EDC

 
that's why G-ds created interpreter directives - being on the first line of the &quot;interprative&quot; script - it 'directs' the calling process to use a given/specified interpreter [shell, perl, ruby etc] for the rest for the context of the file.

In your example you should have the following on the FIRST line of your script:
#!/bin/ksh vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
hi ecasadella, thankx for your suggestion but here they prefer only c-shell.. so somehow i need to do that in c-shell... i'll try again...
 
I just don't get it, I execute this script in my machine on a Solaris 7 with csh and it works perfectly, try to isolate this code into a separate script and tell me step by step what you are doing. EDC
--------------------------------------
This is Unix-Land. In quiet nights, you can hear the Windows machines
reboot.
 
hi ecasadella, below is the script which i'm trying to do.. this script runs every 15 minutes and ftp's to a site to get files... sometimes the ftp session hangs up.. so before invoking this script next time i've to chek whether the previous ftp is still running or not.. if running i've to kill that ftp and start a new one... thanks for your help all the way from y'day...

#!/bin/csh

if ( -f ./myftppid ) then
set oldpid = `cat ./myftppid`
ps -p $oldpid | egrep &quot;ftp&quot; ; set stat = $status
echo $oldpid
while ( $stat == 0 )
# Last invocation of the script still running....
@ x ++
if ( $x == 10 ) then
# tried 10 times to kill it and it wouldn't die
echo &quot;Couldn't kill old process&quot;
echo &quot; &quot;
exit 5
endif
echo &quot;Old FTP still running &quot;
kill -9 $oldpid
sleep 1
ps -p $oldpid | egrep &quot;ftp&quot; ; set stat = $status
end
endif

# put the PID in a file so we can check it next time we start
#echo $$ >! ./myftppid
echo !ps | egrep &quot;ftp&quot; | awk '{print $1}' > ./myftppid

echo ftp -inv \<\< ! > ./tmp5
echo open >> ./tmp5
echo user uname pwd >> ./tmp5
echo cd /temp/temp1 >> ./tmp5
echo get file1 >> ./tmp5
echo quit >> ./tmp5
chmod 744 ./tmp5
./tmp5 > ./tmp66


# remove the PID file if the script complete correctly.
rm -f ./myftppid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top