0

I have a working shell script which calls another shell script to perform some action on some processes running on the server. This inner shell script sometimes prompt to enter the userid and password. If this happens I want to come out this inner script and want to perform kill -9 for the process. Can anyone please suggest on how to achieve this?

One more point, whatever my shell scripts does, I am recording this in a log file,so I assume when script prompts to enter userid and password, this info also get recorded in the log.So is their should be a way to check this in the log file.

I am working on Linux OS. Please check and advise.

user3764852
  • 21
  • 1
  • 4

1 Answers1

0

You can kill your child script after some timeout:

( cmdpid=$BASHPID; (sleep 10; kill -9 $cmdpid) & exec my-child-script )

In this case you will kill my-child-script after given period of time (10 sec).

You can't (easily) detect if you script is waiting for input (on standard input), the only working method is to use strace/ptrace, but it's too complex and I don't think it's really worth it. The timeout-based approach seems to be by far more natural.

You can find here some additional examples of this approach in this question:

Regarding log files:

You can extract data from your log files using grep/sed. To make the answer more concrete, we need some extra data.

Community
  • 1
  • 1
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • The first part of your responses is very helping in terminating the child script when it doesn't complete within the stipulated time which can be specified with the sleep. My another point regarding the log file was to see if doing grep/sed for the log file to check for user input prompt can help me in taking alternative action if the script prompts for user input. – user3764852 Aug 10 '16 at 13:37
  • @user3764852: You could grep for prompt strings for example – Igor Chubin Aug 10 '16 at 14:08