03-27-2014 08:31 AM - last edited on 03-27-2014 06:35 PM by Lisa198503
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?
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
Solved! Go to Solution.
03-28-2014 12:22 PM
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.
03-28-2014 06:52 PM
>something like this:
>[[ $(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
ps -p $BGID | grep -qw $BGID
if [ $? -eq 0 ]; then
03-29-2014 01:12 PM - edited 03-30-2014 08:44 PM
>> 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.
05-07-2014 11:36 AM
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.