Operating System - HP-UX
1752569 Members
5346 Online
108788 Solutions
New Discussion

Re: Options for a timeout variable for FTP (outgoing) sessions?

 
SOLVED
Go to solution
Mike755
Advisor

Options for a time out variable for FTP (outgoing) sessions?

We have FTP disabled (no entry in inetd.conf) for incoming requests but do allow outgoing FTP.  We are running into an issue where sometimes a FTP session (outgoing to various hosts uses different user accounts on internal network) will just hang and not terminate.  Is there a global setting somewhere to have all outgoing FTP sessions terminate after so much time has elasped?

 

Thanks

 

HPUX 11.31 (Shadow mode user authentification)

 

P.S. This thread has been moved from HP-UX>System Administration to HP-UX > networking. -HP Forum Moderator

Thanks, Mike

Legalize Freedom
4 REPLIES 4
Bill Hassell
Honored Contributor

Re: Options for a time out variable for FTP (outgoing) sessions?

What you'll need to do is to create a separate ftp script. Then the batch job timer script will start the script in the background (add & at the end). Immediately after starting the ftp script, capture the background PID from $! and start a timer. You check for the existence of the script every second or two until either the BGPID goes  away or the timer expires, something like this:

 

TIMELIMIT=300
# run the ftp job
ftp_script & BGID=$! while [[ $TIMELIMIT -gt 0 ]] do [[ $(ps -p $BGID | grep -c $BGID) -eq 0 ]] && exit TIMELIMIT=$((TIMELIMIT-1)) sleep 1 done # time limit reached, kill the script # Use kill -15, then -1 finally -9 kill -15 $BGID if [[ $(ps -p $BGID | grep -c $BGID) -gt 0 ]] then kill -1 $BGID if [[ $(ps -p $BGID | grep -c $BGID) -gt 0 ]] then kill -9 $BGID fi fi

The script monitors the PID of the ftp script. When it disappears, the script exits normally. But when TIMELIMIT reaches 0, the loop finishes and it then tries 3 ways to kill the script. The reason for 3 ways is that with all networking tools, hangs can be very difficult to kill. If you want, you can just replace the multiple kills with a single kill -9. I prefer a gentler sequence as standard practice.

 

 

 

 



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Options for a timeout variable for FTP (outgoing) sessions?

>something like this:

 

Some improvements:

>[[ $(ps -p $BGID | grep -c $BGID) -eq 0 ]]

 

Why use "grep -c"?  Why not grep -q -w?  -w for whole PID match.

ps -p $BGID | grep -qw $BGID || exit  # if not found

 

And same for:

if ps -p $BGID | grep -qw $BGID; then

Or:

ps -p $BGID | grep -qw $BGID

if [ $? -eq 0 ]; then

Bill Hassell
Honored Contributor
Solution

Re: Options for a timeout variable for FTP (outgoing) sessions?

>> Why use "grep -c"?  Why not grep -q -w?  -w for whole PID match.
>> ps -p $BGID | grep -qw $BGID || exit  # if not found

This is a force of habit because grep -w did not exist before HP-UX 11.11 so I use -c.

Also, ps -p is an exact match (there will be exactly zero or one occurences of the BGID).

The grep is to ignore the header line. 

 

So the first test:

    [[ $(ps -p $BGID | grep -c $BGID) -eq 0 ]] && exit

 

can be futher simplfied to:
    ps -p $BGID >/dev/null || exit

 

and the subsequent tests for kill success can be this:

         if ps -p $BGID >/dev/null; then

 

Years ago, I read that in Unix, if you can't do the same thing at least 3 different ways, you haven't tried hard enough.

So rewritten, it would look like this:

 

TIMELIMIT=300
# run the ftp job
ftp_script &
BGID=$!
while [[ $TIMELIMIT -gt 0 ]]
do
   ps -p $BGID >/dev/null || exit
   TIMELIMIT=$((TIMELIMIT-1))
   sleep 1
done

# time limit reached, kill the script
# Use kill -15, then -1 finally -9
kill -15 $BGID
if ps -p $BGID >/dev/null; then
   kill -1 $BGID
   ps -p $BGID >/dev/null && kill -9 $BGID
fi

 I turned this into a function and tested it as a tool for handling processes/scripts that may hang.

Since it puts the process into the background, stdout and stderr should be redirected or changed to a logfile or use logger to keep records of the process activity.

 



Bill Hassell, sysadmin
Mike755
Advisor

Re: Options for a timeout variable for FTP (outgoing) sessions?

You are the man Bill....I remember you way back if this is the same Bill been our here for over a decade.  Thanks for the information greatly appreciated.  Hope all is well for you and your career.

 

Mike Bray

Thanks, Mike

Legalize Freedom