Listing 1
1: #
2: # bgrun.sh/bgrun2.sh (linked):
3: # Run a process in the background.
4: #
5: # Written by Leor Zolman, 7/90
6: #
7: # Formal usage:
8: # bgrun.sh outfile [ -l lockfile ]
9: # bgrun2.sh outfile [ -l lockfile ] 2>errorfile
10: #
11: # Takes script to be executed from standard input.
12: # bgrun.sh: stdout and stderr written to outfile.
13: # bgrun2.sh: stdout written to outfile, stderr passed (to errorfile)
14: #
15: # If BGTEST environment variable is "Y", creates the script file in
16: # the current directory for debugging and does not remove it after execution.
17: #
18: # example:
19: #
20: # bgrun.sh /tmp/output <<END
21: # echo this is a test of background processing
22: # l
23: # echo this is the end.
24: # END
25: #
26: # WARNING!!!!! Remember to escape special characters ( $, `) that
27: # must be interpreted in the background scripts!
28: #
29:
30: if [ "$BGTEST" = Y ]; then
31: debug=Y
32: else
33: debug=N
34: fi
35:
36: if [ $# -ne 1 -a $# -ne 3 ]; then # check for proper usage
37: echo usage: $0 outfile [-l lockfile]
38: exit
39: fi
40:
41: trap "rm $script; exit 1" 1 2 3 9 14 15
42:
43: if [ $debug = Y ]; then
44: script=script # debug script in current directory
45: else
46: script=`tmpname bg` # place script in /tmp diretory
47: fi
48:
49: if [ $# -eq 3 ]; then
50: case $2 in
51: -l|-L)
52: echo "trap \"rm $script; rm $3; exit 1\" 1 2 3 9 14 15" >$script;;
53: *)
54: echo "usage: $0 outfile [-l lockfile]"
55: exit 1;;
56: esac
57: else
58: echo "trap \"rm $script; exit 1\" 1 2 3 9 14 15" >$script
59: fi
60:
61: cat >>$script # copy std input into the script file
62:
63: [ $debug = N ] && cat >>$script <<-END # and append a "selfdestruct" to
64: rm $script # remove the script after execution
65: END
66: chmod +x $script # make the script executable
67:
68: [ -f $1 ] && rm $1 # erase old output file, if any
69:
70: cat </dev/null >$1 # ensure output file is writeable
71: chmod 666 $1 # (i.e., erasable) by everyone, to
72: # allow public output files
73:
74: unset redirerr
75: [ $0 = bgrun.sh ] && redirerr="2>&1" # If bgrun.sh, direct stderr to stdout
76: nohup $script >>$1 $redirerr & # execute script (immune to hangup)
|