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!

Hi TDATGOD.. Regarding Your script

Status
Not open for further replies.

nithin97

Programmer
Oct 23, 2002
25
US
Hi TDATGOD,

Thanks for your awesome reply... Actually I'm new to Unix Shell Scripting.. Can you please clarify me some doubts in ur script.... Sorry if I am disturbing you again..

Doubt 1 :
the following line gives syntax error saying
Error message : if: Expression syntax
Error line : if ( -f cat /tmp/myftppid )

Doubt 2 :
what does this following line do ?
| egrep "myftpscript"

and what does this following loop do ?
@ x ++
if ( $x == 10 ) then
# tried 10 times to kill it and it wouldn't die
echo "Couldn't kill old process"
echo " "
exit 5

Thank you very much.... and below is ur script...

if ( -f cat /tmp/myftppid ) then
set oldpid = `cat /tmp/myftppid`
ps -p $oldpid | egrep "myftpscript" ; set stat = $status
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 "Couldn't kill old process"
echo " "
exit 5
endif
echo "Old FTP still running "
kill -9 $oldpid
sleep 1
ps -p $oldpid | egrep "myftpscript" ; set stat = $status
end
endif

# put the PID in a file so we can check it next time we start
echo $$ >! /tmp/myftppid

rm -f /tmp/myftppid




 
Hi,
Sorry remove the word CAT. I cut and pasted from the line below and cut too much. Also I apologize if the syntax is a little off since I did this off the top of my head. Also I did it in CSH since you said you were using CSH.

#! /bin/csh

if you are using SH or KSH to script in the script will have to be modified.

----------------------

ps -p $oldpid | egrep "myftprogram" ; set stat = $status

means take the old PID and ask PS to find the process with that pid then take that line and pass it to egrep which will search for the word "myftpprogram" ( you must use the name of your script here in both places in the script.

UNIX can reuse PIDs so it is possible that the PID of your old program from 15 minutes ago is now being used by a differnt program. This ensures that the PID we are about to KILL is really "your FTP program" and not some other task you may have started.



------------------------------


Since I used a While loop to keep sending KILL -9 to your old process what is to prevent it from Looping indefinately if your program won't Die. I guess one could argue that if the first kill -9 doesn't kill the program than sending 9 more ( 10 in all ) won't help. after 10 attempts to kill the old process the script given up.

I chose to exit, you might decide to BREAK and run the new download even though you couldn't kill the HUNG download pending from the last go around.




 
Hi TDATGOD, Regarding ur script... U r getting the PID of this process and putting it in a file... but what i want to know is how to get the PID of that ftp process alone(pls take a look at the script below).. 'coz rite now this following line,
echo $$ >! ./myftppid
puts the PID of this whole script and puts that in that specified file.. and kills it next time.. but the ftp process is not killed its still running.... so we need to take the PID of that ftp process alone and kill it.. can u pls help me out ?

#!/bin/csh

if ( -f ./myftppid ) then
set oldpid = `cat ./myftppid`
ps -p $oldpid | egrep "test1.csh" ; 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 "Couldn't kill old process"
echo " "
exit 5
endif
echo "Old FTP still running "
kill -9 $oldpid
sleep 1
ps -p $oldpid | egrep "test1.csh" ; set stat = $status
end
endif

# put the PID in a file so we can check it next time we start
echo $$ >! ./myftppid

########### MY FTP SCRIPT FOLLOWS ###########

echo ftp -inv \<\< ! > ./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