The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
executing Unix script that contains CURL FTP commands from Peoplesoft Process Schedlr sfedak Shell Programming and Scripting 2 02-18-2009 02:20 PM
child process state smreddy UNIX for Dummies Questions & Answers 8 12-24-2007 12:51 AM
The State of Unix (Addict 3D) iBot UNIX and Linux RSS News 0 06-18-2007 10:40 PM
The state of Unix - ZDNet.com blogs iBot UNIX and Linux RSS News 0 06-18-2007 03:40 AM
Process State ianlow UNIX for Dummies Questions & Answers 1 09-06-2006 10:11 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
egiz81 egiz81 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Unix script (sh): state of ftp process

Hi guys,
I’m writing a script in which I have to get file from a remote host by ftp. The problem is that the remote machine could be very slow, not connected or ok. To resolve this problem, I write this:

[……]
echo "verbose on" > ftprap.cmd
echo "prompt " >> ftprap.cmd
echo "ascii" >> ftprap.cmd
echo "passive off" >> ftprap.cmd
echo "mget * " >> ftprap.cmd
echo "quit" >> ftprap.cmd

ftp %IP < ./ftprap.cmd > ftp.log 2>&1 &
PID_FTP=`echo $!`
sleep 60
ps -o pid,tty,time,cmd,state|grep -E "$PID_FTP%S" >/dev/null 2>/dev/null

if [ $? = 0 ]
then
kill -9 $PID_FTP >> ftp.log 2>&1
fi
[……]

To kill ftp only if it is blocked.

It happens that even if ftp runs, ps shows me with state = S, that is sleeping!!! I try to run the script with “sleep 1”, while it’s transferring a lot of file, and I’m sure of this: ps shows ftp is sleeping and then the script kills it.
I’m not so very expert about unix scripting, so I don’t’ know where I make mistakes.

Would you help me? Thanks in advanced.
  #2 (permalink)  
Old 05-27-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,789
Sleeping does not mean that your program is doing nothing, it just means that it's not currently being run on any processor. For example, on a machine with only 1 processor you'll only see 1 program marked as running with ps (and that's probably ps itself). With 2 processors you'll see 2, ...

An IMHO better way to check connectivity would be to first try to ping the host, check if the FTP port is open, try to download a small check file, and only then do the large transfer.
  #3 (permalink)  
Old 05-27-2009
egiz81 egiz81 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Unhappy

Hi pludi,
do you mean that if I use sleep I block my ftp command? According to avoid to wait forever if the remote host doesn’t work, I want to implement a sort of timeout, after this, if the ftp is blocked, I’ll kill it. If I use sleep, is ftp command in background stopped??? If it’s true, I’m a quite worried… I try to ping first the host (in the real case this code is in a loop for a list of hosts) but so many firewall bock ping and if ping has a positive result, this doesn’t unsure that ftp runs.


... I'm spending a lot of time about this problem.. and I'm getting crazier than I'm just!
  #4 (permalink)  
Old 05-27-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,789
No, your sleep does not block the FTP command. Let's sidetrack into kernel process management for a bit:

Since their invention CPU cores can only execute one process at a time. This meant that a single process waiting for user input could block the whole system. So someone came up with the idea of time slices. Each process is allowed execution for a certain time. After that time it returns control to the kernel. All was good until some processes didn't return control on purpose.
Most modern kernels use preempting instead. Again, each process is allocated execution time. After that time the OS kernel is woken via a timer interrupt (or sooner if the process gives it's rights back because it's waiting for something), the first process is sent into a sleep mode and another is given CPU time. The first process will continue when it's its turn.

Now back to your case: you're starting the FTP process and send it into the background. It gets its time-slices just like any other process, but it's not executing the whole time. Sometimes other processes are executed, so ftp is marked as 'sleeping' or, better said, waiting for its turn again. If you only have one CPU core and get the current list with ps, all processes except for ps will (most likely) be marked as 'sleeping', since ps is currently using its allocated time to gather the stats.

My suggestion for your problem would be:
  1. ping the host in question (maybe mark those pingable accordingly and skip the others)
  2. test if the FTP port is open. You can either use netcat (eg: netcat -z $host 21 and check $?) or parse the output of nmap)
  3. create a small checkfile on the FTP server, and fetch that first. For example, start the FTP command for that in the background. If it's still running after 5 seconds, consider the host dead.
  4. if all (necessary) checks are good, continue with the large transfer
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0