LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute a script and not able to redirect to a file (https://www.linuxquestions.org/questions/linux-newbie-8/execute-a-script-and-not-able-to-redirect-to-a-file-4175424869/)

shisha 08-30-2012 07:57 PM

Execute a script and not able to redirect to a file
 
Hi,

I am trying to execute a script. This scripts waits for commands from another console. I want to capture everything that will happen from this script.

I am using the following:
./script >> log.txt
cat log.txt


log.txt is empty.


If I use something like this:
ps >> log.txt , it works.


Thanks!

konsolebox 08-30-2012 08:48 PM

What's the contents of script?

shisha 08-30-2012 09:33 PM

The script's function is to execute abc.out. To simplify, abc.out has a bunch of questions and the user has to answer it.

Contents of the script:

pidof abc.out | xargs kill (find the pid and kill the process)
/programs/abc.out

The function of abc.out is:
abc.out asks the user many questions and the user replies to it.
abc.out waits for the inputs from the user.

If the script is to execute many commands without any user interaction the redirection to the file works but if its the case like above, the redirected file is empty.

Thanks!

P.S: Since I use Busybox, and there is no pidof, I somehow managed to do the equivalent of pid.

casualfred 08-30-2012 10:01 PM

You could probably combine all of this into one script, if abc.out is also a script...
You could run something like this as a script maybe:
Code:

#!/bin/bash

echo "This is the answer log" > log.txt
echo "" >> log.txt

clear
echo This is question one?
read QONE
echo $QONE >> log.txt

clear
echo This is question two?
read QTWO
echo $QTWO >> log.txt

clear
echo This is question three?
read QTHREE
echo $QTHREE >> log.txt

clear

I don't know if this was actually what you were looking for, but it's just a thought :)

shisha 08-30-2012 10:07 PM

Hi Casualfred,

Thanks for your input. But abc.out does a lot more than just simple questions. It communicates between another console where it gets the input. So this has to be run by another script. And when I run this script I need to capture all the input/output from abc.out to a log file. Hence my question.

Thanks!

konsolebox 08-30-2012 10:37 PM

Does it work if you run it directly?
Code:

/programs/abc.out >> log.txt
I mean run it in an interactive shell, and not as wrapped in a script.

shisha 08-30-2012 10:39 PM

No it doesn't. It's still the same issue. The script is just to kill the process before executing it.

konsolebox 08-30-2012 10:41 PM

Quote:

Originally Posted by shisha (Post 4768789)
No it doesn't. It's still the same issue. The script is just to kill the process before executing it.

I guess the problem lies on abc.out itself. Perhaps it makes an output to another virtual device e.g. /dev/stderr, /dev/vc/somewhere, etc.

Try
Code:

/programs/abc.out >> log.txt 2>&1

konsolebox 08-30-2012 10:45 PM

Or it doesn't create an output at all. Perhaps the application only creates an output on virtual terminals just for the sake of interaction. It doesn't open anything like files if it's not.

shisha 08-31-2012 12:10 PM

I tried the same but it doesn't work :(

Quote:

Originally Posted by konsolebox (Post 4768794)
Or it doesn't create an output at all. Perhaps the application only creates an output on virtual terminals just for the sake of interaction. It doesn't open anything like files if it's not.

But I can see a lot of information coming up in my console.

shisha 08-31-2012 12:32 PM

When I use the following:

Code:

./script & >> log.txt
cat log.txt

I am now able to execute the script but I get the following output :
Code:

[1] + Done                            ./script

konsolebox 08-31-2012 07:48 PM

Quote:

Originally Posted by shisha (Post 4769360)
When I use the following:

Code:

./script & >> log.txt
...

I didn't see you use "&" with script before.. Why run it in the background?

shisha 08-31-2012 08:20 PM

No I just wanted atleast the script to run while having the ">>" in my command. But I don't want it to run in the background.

Also, my task can also be implemented if I could capture everything that is going to happen right from logging into the console. Is there anyway to capture everything from log on. I will be continuously monitoring the log so that once I find a regex match I will have to run the script again. That has been my aim.

Thanks !

konsolebox 08-31-2012 09:58 PM

Quote:

Originally Posted by shisha (Post 4769341)
But I can see a lot of information coming up in my console.

Try running vim > out.txt. You'll notice that it would send a message like "Vim: Warning: Output is not to a terminal. This shows that an application could detect what its output device is like. Perhaps your application abc.out doesn't open any output at all if it's not a terminal. Could you examine the application directly, I mean examine its source code?

shisha 08-31-2012 10:15 PM

Busybox that I use doesn't support vim.


All times are GMT -5. The time now is 09:40 AM.