Unix script (sh): state of ftp process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix script (sh): state of ftp process
# 1  
Old 05-27-2009
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  
Old 05-27-2009
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  
Old 05-27-2009
Data

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! Smilie
# 4  
Old 05-27-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (6 Replies)
Discussion started by: naveeng
6 Replies

2. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (1 Reply)
Discussion started by: naveeng
1 Replies

3. BSD

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (0 Replies)
Discussion started by: naveeng
0 Replies

4. Shell Programming and Scripting

Script for Deleting a process which exist every day in hold state

Hi All , There always exist one process in hold state every day which will cause savior impact if i didn't kill it. i will do it manually . Manul process is this. sudo -iu root/opt/app/root/cdlinux/ndm/bin/direct -- it will take me connect direct prompt Sel proc ; -- it will displays... (9 Replies)
Discussion started by: Phani369
9 Replies

5. Shell Programming and Scripting

Problem with a script for checking the state of a process

Hello Everyone, I have a process that should be always running. Unfortunately, this process is getting down almost every 10 minutes. I want to make a script that verify the state of this process: If the process is up, the script shouldn't do nothing and if it's down he should run it. Can... (3 Replies)
Discussion started by: adilyos
3 Replies

6. Shell Programming and Scripting

Script to Kill process which is in hang state

Hi, Can anyone help to create a script that will kill the process which is in hang state. (1 Reply)
Discussion started by: A.Santhosh
1 Replies

7. UNIX for Advanced & Expert Users

When a process will go to 'D' state?

I'm using "Linux hostname 2.6.28-15-generic #49-Ubuntu SMP Tue Aug 18 18:40:08 UTC 2009 i686 GNU/Linux" All the client machines will use Thin-client ,I will use my laptop for working and I will mount my home directory from server to my laptop. If I open the firefox in my laptop the... (1 Reply)
Discussion started by: ungalnanban
1 Replies

8. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

9. UNIX for Dummies Questions & Answers

child process state

Hi all, I have one requirement,I have two shell programs one is parent and the other one is child . from parent script i need to execute/trigger/call child script as background job. my requirement is after calling child script i want the child process information i.e PID of child weather it is... (8 Replies)
Discussion started by: smreddy
8 Replies

10. UNIX for Dummies Questions & Answers

Process State

If your process makes a system call, then while the system call code is being run in the kernel, is your process READY, RUNNING or BLOCKED? (1 Reply)
Discussion started by: ianlow
1 Replies
Login or Register to Ask a Question