Operating System - HP-UX
1836444 Members
2598 Online
110100 Solutions
New Discussion

Re: shell script challenge regarding background/prompt/pipe

 
SOLVED
Go to solution
Kevin_31
Regular Advisor

shell script challenge regarding background/prompt/pipe

Hey experts. Is it as slow in the US as it is here in the UK now (just before Christmas)?

Anway, points for the genius who can make sense of the quagmire of concepts involved scripting the equivalent of pressing return at the end of a command which runs in background.

I need to run a script to start up a program after Omniback finishes a backup. We do this all the time here, but this time the program runs in background and the script doesn't return the prompt (return code?) therefore omniback thinks the script has failed when it eventually reaches it's timeout.

Running the script interactively I can see that it's kicked off the background process, e even echoes stuff which I stuck in AFTER the bg process. However, it doesn't give me the prompt back until I press return.

So, if I could send "^M" or echo "" or something similar, then connect it to the offending program with a pipe or "<" or something, I'd be in business.

I've looked up googlegroups and found some interesting discussions, but nothing which addressed my problem (either directly or indirectly).

I've tried stuff like:

echo ^M | nohup EXECUTABLE &
( echo ^M | nohup EXECUTABLE & )
echo "" | nohup EXECUTABLE &
echo "" | (nohup EXECUTABLE ) &
nohup EXECUTABLE & < echo ""
nohup EXECUTABLE & | xargs echo ""



I'm gonna be left with no choice but to take this bit o' background code out of the post-exec Omniback script and instead put it in a separate cron-job. This is untidy and not what I really wanna settle for.

So, I've come here... To the Great Source of all Knowledge and Wisdom!

cheers,
Kevin
18 REPLIES 18
Elmar P. Kolkman
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

Have you tried this:

nohup EXECUTABLE
where tmpf contains an empty line?


Every problem has at least one solution. Only some solutions are harder to find.
Graham Cameron_1
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

nohup EXECUTABLE
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Mark Grant
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

I imagine that if you had a wrapper script as your omniback postexec command that ran the real script in the background you'd be OK.

omnibackpostexec script would do soemthing like this


nohup script2 &
exit 0

If you really want to go mad you need to fork and exit and for that, lets do perl.

Your omniback post exec script would look like

#!/usr/bin/perl

$PID=fork;
if($pid != 0){
exit 0;
}

`/my/shell/script/here`;



Of course, if you fancy doing your script in perl, then you don't need to run that shell script at the bottom.

Never preceed any demonstration with anything more predictive than "watch this"
Kevin_31
Regular Advisor

Re: shell script challenge regarding background/prompt/pipe

Elmar & Graham, tried it but no success.
Mark, tried the first suggestion, still no-go. I'd be interested in trying a fork in shell script, but don't really wanna use perl.
Mark Grant
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

Kevin,

I don't think you can "fork" in a shell but A.Clay Stephenson is dropping his scripting bombs again today so he may well prove me wrong!

Your problem is not the second command (though if it were written correctly you wouldn't have this problem) but the shell that is running it. This is why I can't quite understand why the first option I suggested doesn't work.

However, that perl thing will do nicely as an omniback post exec script (I do something similar myself) and you'll never have to touch perl aain if you didn't want to :)
Never preceed any demonstration with anything more predictive than "watch this"
Massimo Bianchi
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

much, much simpler...

nohup (EXECUTABLE << EOF

EOF) &


Massimo


Kevin_31
Regular Advisor

Re: shell script challenge regarding background/prompt/pipe

EXECUTABLE: Syntax error at line 1 : `(' is not expected.
Kevin_31
Regular Advisor

Re: shell script challenge regarding background/prompt/pipe

this program must be way-dodgy. I've wrapped up the program and flags/parameters into one script, executed it within another one, using Massimo's suggestion, but still it's a no-go.

Thanks everyone. Maybe i'll try the perl option afterall... back soon.

Massimo Bianchi
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

Let's tear away the parentesis:

####################
EXECUTABLE:

#!/sbin/sh

do_something << EOF

EOF

#######################


nohup EXECUTABLE &



Massimo
Graham Cameron_1
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

Suspect your program does a close(0) and then opens another fd for reading.

Unless you know which fd this is, or have the patience to keep trying

nohup EXECUTABLE 3nohup EXECUTABLE 4
etc up to 20,

then I'd give up if I were you...

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
BIHAN
Frequent Advisor

Re: shell script challenge regarding background/prompt/pipe


mkfifo fifo
nohup EXECUTABLE echo "^M" >fifo
BIHAN
Frequent Advisor

Re: shell script challenge regarding background/prompt/pipe

missing an & at the end of the nohup line

i think
echo > fifo
would be better than
echo "^M" >fifo
James Lynch
Valued Contributor

Re: shell script challenge regarding background/prompt/pipe

Kevin,

How about posting this script so that we can have a look at it?

JL
Wild turkey surprise? I love wild turkey surprise!
Kevin_31
Regular Advisor

Re: shell script challenge regarding background/prompt/pipe

The real issue is the EXECUTABLE, which is part of Oracle Applications 11i Server, and is over 6 megabytes in size. Probably not the best idea to post (I expect it wouldn't run without the other bits of code that make up the Oracle installation.

Thanks everyone, but I think I'll just have to use cron to restart this bit and keep it out of Omniback post-exec alltogether.

cheers
Mark Ellzey
Valued Contributor
Solution

Re: shell script challenge regarding background/prompt/pipe

Kevin,

If you are just trying to answer a prompt that the application requires, and it's a carriage return, I've done it as such:
echo "\013" | APPLICATION > /dev/null 2>&1 &

This works for me in a script that runs an Informix backup procedure, which is normally interactive.

Regards,
Mark
Kevin_31
Regular Advisor

Re: shell script challenge regarding background/prompt/pipe

Amazing, I've tried every suggestion listed above, and the last one... finally... worked!

cheers Mark!

(I can always count on ITRC Geniuses)
Todd McDaniel_1
Honored Contributor

Re: shell script challenge regarding background/prompt/pipe

No points here...

I just want to say I enjoyed reading this puzzle...

We need more posts like this to test our brains power.

I personally troll the boards to educate myself... and posts like this are helping.
Unix, the other white meat.
Ulrich Deiters
Frequent Advisor

Re: shell script challenge regarding background/prompt/pipe

... sorry to be late due to my timezone! A useful trick to communicate with a background process works like this:

1st: You start the background job:
bgjob 2>&1 1>logfile |&

2nd: You retrieve the background job's stderr output (error messages, prompts, etc.):
read -p $bg_output

3rd: You send data to the background job's stdin:
bg_input="some_input"
print -p -R $bg_input

This works with POSIX-compatible shells, e.g., /usr/bin/sh and /usr/bin/ksh, but not with bash.

I hope this is useful for you.