[Wylug-help] Bash Scripting question.
Frank Shute
Frank Shute <frank at esperance-linux.co.uk>
Thu, 27 Feb 2003 01:29:57 +0000
On Wed, Feb 26, 2003 at 03:28:31AM -0700, Thomas, Nicholas wrote:
>
> This message is in MIME format. Since your mail reader does not understand
> this format, some or all of this message may not be legible.
> --
> [ Picked text/plain from multipart/alternative ]
>
> Hi,
>
> I have a bash script to write and it would be nice if it had the following
> attributes:
>
> 1) A simple script which throws out text messages to the console when
> running in the foreground, using the echo command.
> Whilst simultaneously sending the same output to a log file.
>
> 2) The script can be sent to the background (Using Ctrl-Z then 'bg' ). The
> log file still gets its message text, but the console no longer
> gets any messages. The script continues to run in the background.
>
> 3) I can resume foreground operation, using 'fg', and the console messages
> start coming out on the console again. I am not worried about losing
> the console messages which were generated while the script was in the
> background because I can review the log file for the complete list.
>
> I presume this is possible?
>
> Kind Regards
>
> Nick Thomas
> --
Following up on my earlier post suggesting trapping signals, I wrote
this small demo. I use BSD sh but the principles (& syntax) are much the
same for bash by the looks of things:
<----------------->
#!/bin/sh
# Grab functions
# Use source builtin for bash
. ~/shellfuncs/myfuncs
#
# Those functions:
#
#
# setlog () {
# while true; do
# sleep 5
# echo "Hello logfile!" >> ~/logfile
# done
# }
#
# setcon () {
# while true; do
# sleep 5
# echo "Hello console & logfile!" | tee -a ~/logfile
# done
# }
trap setcon USR1
trap setlog USR2
# Set up a loop...
while true; do
sleep 5
setcon
done
trap - USR1 USR2 # reset signal handlers
<----------------->
Then:
$ ./demosig &
[2] 20618
Hello console & logfile!
Hello console & logfile!
Hello console & logfile!
..
$ kill -USR2 20618
$ tail -f ~/logfile
Hello console & logfile!
Hello console & logfile!
Hello console & logfile!
Hello logfile!
Hello logfile!
^C
$ kill -USR1 20618
Hello console & logfile!
Hello console & logfile!
Hello console & logfile!
..
Isn't it easier to just use tail? ;)
You might also want to have a look at GNU screen.
BTW, kill & tee might be a bit different on Linux - can't remember.
I also cheated a bit and used a couple of xterms rather than vc's.
(The program output gets in the way of interacting with the shell).
--
Frank
*-*-*-*-*-*-*-*-*-*-*
Boroughbridge.
Tel: 01423 323019
---------
PGP keyID: 0xC0B341A3
*-*-*-*-*-*-*-*-*-*-*
http://www.esperance-linux.co.uk/
"There's plenty of women involved in the Linux community, but I would
imagine few of them are hanging out with a bunch of stinky
sandal-clad freaks in some basement, reading Linux Journal and
throwing darts at a picture of BillG."
- assessment of LUGs on k5