Re: Options for a timeout variable for FTP (outgoing) sessions? (152 Views)
Reply
Advisor
Mike755
Posts: 16
Registered: ‎01-10-2013
Message 1 of 5 (193 Views)
Accepted Solution

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

[ Edited ]

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
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,181
Registered: ‎05-29-2000
Message 2 of 5 (160 Views)

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.

 

 

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,779
Registered: ‎03-06-2006
Message 3 of 5 (157 Views)

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

Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,181
Registered: ‎05-29-2000
Message 4 of 5 (152 Views)

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

[ Edited ]

>> 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.

 

Please use plain text.
Advisor
Mike755
Posts: 16
Registered: ‎01-10-2013
Message 5 of 5 (121 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation