Detecting if command is waiting for input


 
Thread Tools Search this Thread
Top Forums Programming Detecting if command is waiting for input
# 1  
Old 08-07-2008
Detecting if command is waiting for input

Hi,

After doing a fork and executing a shell, we execute (third party) commands which are essentially non-interactive. But some of them ask for input, under some (unforeseeable) circumstances. When this happens we go on waiting for output. Their is timeout, of course, but we don't seem to know if the process is actually doing some processing or is just simply waiting on us. Just wanted to ask if it is possible by some trickstery to find is the command is waiting for input. Note we can't parse the command output to check if it is waiting on input, because it varies across commands and becomes unmaintainable.

One possible approach that I can think of is looking out if the process is waiting on the stdin file descriptor. Could somebody elaborate as to how do I proceed in this direction.

thanks!
# 2  
Old 08-07-2008
Your forking parent should fetch its children from the proc table.
If it's a mere shell script it could call wait [pid].
If it's a program or script written in an IPC capable language
you should call waitpid(). Usually one would implement a signal handler
that automatically fetches its children exit codes on SIGCHLD within a loop or so.
To see whether your child is expecting input from stdin you could for instance strace or tusc by attaching to its PID and watch the syscalls.
# 3  
Old 08-07-2008
Redirect their standard input from /dev/null.

Code:
command </dev/null

# 4  
Old 08-07-2008
Quote:
Originally Posted by buffoonix
Your forking parent should fetch its children from the proc table.
If it's a mere shell script it could call wait [pid].
If it's a program or script written in an IPC capable language
you should call waitpid(). Usually one would implement a signal handler
that automatically fetches its children exit codes on SIGCHLD within a loop or so.
To see whether your child is expecting input from stdin you could for instance strace or tusc by attaching to its PID and watch the syscalls.
Could you elaborate on the strace or tusc part. As per my understanding they are unix commands, however I need to do something programmatically because we are doing a C fork in calling the command. Further this needs to be as efficient as possible because, as far as I can understand this would require polling, unless there is some way to signal the parent process that child is waiting on the input. Note that we have no control on the child process, in a sense that we can't change is to send the signal or anything (they are external binaries).
# 5  
Old 08-07-2008
Quote:
Originally Posted by era
Redirect their standard input from /dev/null.

Code:
command </dev/null

Thanks! Yes this should work for the wait part. Smilie

But is there still a way to detect if the process is waiting on input?
Because if this is possible this will really help us in the long run.
# 6  
Old 08-08-2008
Quote:
Originally Posted by slash_blog
Thanks! Yes this should work for the wait part. Smilie
I realize even this wont work for programs that use ttys instead of stdin for input.

For example sudo prompts for password and doesn't use stdin. Any ideas??
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

2. Shell Programming and Scripting

How to detect that foreground process is waiting for input?

Hi, I have to create a script (ksh or perl) that starts certain number of parallel jobs (another scripts), each of them runs as a foreground process in a separate session. Plus I start monitoring job that has to determine if any of those scripts is expecting input from operator, and switch to... (4 Replies)
Discussion started by: andyh80
4 Replies

3. UNIX for Dummies Questions & Answers

passing input into a cmd line that's waiting for a response

Hi, I am writing a little script to update a parameters on JMQ. however the JMQ requires a "y" confirmation to be input as part of the cmd I am running. However I want run this script to offline with no input from a user. it works if a I create a file with with just y in it and pass that in... (3 Replies)
Discussion started by: shropshirehobbi
3 Replies

4. Shell Programming and Scripting

read command not waiting

Dear All, read command not waiting for my input, please suggest. code: while read line do echo "$line " | grep -i .par$ if ; then cd ../par echo " Do you want to proceed" read else cd ../sql read fi done <inp.txt Its not asking me any input? (2 Replies)
Discussion started by: mahendra singh
2 Replies

5. Shell Programming and Scripting

How to skip command if it is hanging while waiting for response

Hello, I have a script that contains the command "whois 1.2.3.4" Sometimes this command takes far too long to produce any output and as a result the rest of the script is not executed. Can anyone suggest a method so that if no output is produced after say 2 seconds the script skips that... (2 Replies)
Discussion started by: colinireland
2 Replies

6. UNIX for Dummies Questions & Answers

Send email with attachment and body : mailx , waiting for input , signal Control D

Hi, I am trying to send email with attacment and body using "mailx" (cat body.txt; uuencode attach.txt) | mailx -s "Attachment" abc@xyz.com When i type this command, the shell is still waiting for me to enter something in standard input and press control D before it sends a mail and... (2 Replies)
Discussion started by: aliaszero
2 Replies

7. Shell Programming and Scripting

SQL PLUS Command 'ACCEPT' is not waiting for user input with sh shell script

Dear All, The sqlplus 'Accept' command is not waiting for user input when I include the command within a shell script. Note: The 'Accept' command is working fine if I execute it in a SQLPLUS Prompt. Please fins the below sample script which i tried. SCRIPT: -------- #!... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

9. Programming

Detecting Keyboard Input without return

Hi, I need a way to detect the up and down arrow key inputs for my program. I do not want to wait for the return key to be entered(so that rules out getch() and family). Also I need to process several of these inputs in parallel, by servicing each request with a thread. Is that possible? ... (4 Replies)
Discussion started by: ravneetd
4 Replies
Login or Register to Ask a Question