I am trying to use script
command for logging a bash session.
The script
command is executed from withing a bash script but as soon as it is executed, the bash script terminates.
I have tried to invoke the command using various combination always with the same result (termination of the bash script as soon as the command is called). The output that I get is the following:
Script started, file is typescript root@ubuntu: ...
I have also tried to invoke the command with an &
in the end but again with no luck.
Can anyone tell me how should I invoke the command from a bash script?
Thanks
To write output of Bash Command to Log File, you may use right angle bracket symbol (>) or double right angle symbol (>>). Right angle braketsymbol (>) : is used to write output of a bash command to a disk file. If the file is not already present, it creates one with the name specified.
Your shell script did not terminate. It is still running. You are getting a prompt because script
is spawning a new shell. the prompt you see is the prompt from the spawned shell.
The normal use case for script
is something like this:
script
. this spawns a new shell.script
So basically script
is working as expected. You will have to find another way to achieve what you want.
You can log the execution of your script like this:
#! /bin/bash exec > logfile 2>&1 set -x FOO=BAR echo $FOO
Explanation:
exec > logfile 2>&1
redirects stdout and stderr to logfileset -x
makes bash print every command before executing itExample:
$ ./foo.sh # (no output here because everything goes to logfile) $ cat logfile + FOO=BAR + echo BAR BAR
Disadvantage of this method is that the script prints no output for humans to see. Everything goes to the logfile.
Alternatively you can do it like this:
#! /bin/bash # nothing special here FOO=BAR echo $FOO
Then execute like this:
$ script -c "bash -x foo.sh" Script started, file is typescript + FOO=BAR + echo BAR BAR Script done, file is typescript
now output is directly visible and also saved to logfile (default name of logfile is typescript
)
$ cat typescript Script started on Mi 18 Mai 2011 01:05:29 CEST + FOO=BAR + echo BAR BAR Script done on Mi 18 Mai 2011 01:05:29 CEST
Your bash script is still running, but it has spawned a new interactive shell. The bash script is waiting for script
to complete, which will only happen when the interactive shell is terminated (either by being killed, or by the user typing exit
).
To make the command after script
be logged by script
, do it like this:
script build_log -c 'echo -e "* This line should appear inside the /"build_log/" log file..."'
However, script
will stop running after running that command.
To run multiple commands inside script
, put those commands inside another bash script and specify that bash script as the command to run to the -c
option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With