-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatahup
executable file
·90 lines (78 loc) · 2.59 KB
/
statahup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
# statahup will prepare and submit a Stata-SE/MP job and email the user when it
# is finished
# new to 2.1 - added option functionality
# - added option to suppress email
# - added option to use stata-mp instead of stata-se
# - adjusted how errors are handled
### Setup options; if incorrect, notify of error and exit
email=yes
version=stata-se
while getopts ':em' opt ; do
case $opt in
e) email=no ;;
m) version=stata-mp;;
\?) echo "Invalid option: -$OPTARG"
exit ;;
esac
done
# skip over the processed options
shift $((OPTIND-1))
### Test if version exists
if ! hash $version; then
echo -e "\n $version does not exist \n"
exit
fi
### Test usage; if incorrect, output correct usage and exit
if [ "$#" -ne 1 ]; then
echo "********************************************************************"
echo "* Statahup version 2.1 *"
echo "********************************************************************"
echo -e "The 'statahup' script submits Stata batch jobs using nohup. \n"
echo -e "usage: statahup [-em] file.do \n"
echo -e "Spaces in the filename or directory name may cause failure. \n"
exit
fi
# Stem and extension of file
filestem=`echo $1 | cut -f1 -d.`
extension=`echo $1 | cut -f2 -d.`
### Test if file exist
if [[ ! -r $1 ]]; then
echo -e "\n File does not exist, or option mispecified \n"
exit
fi
### Test file extension
if [[ $extension != do ]]; then
echo -e "\n Invalid input file, must be a do-file \n"
exit
fi
### Main file
# Direct output
output=$filestem.log
echo "stata: directing output to $output"
# Use user-defined 'TMPDIR' if possible; else, use /tmp
if [[ -n $TMPDIR ]]; then
pathy=$TMPDIR
else
pathy=/tmp
fi
# Tempfile to enable emailing content
timer=`mktemp $pathy/timer.XXXXXX` || exit 1
chmod 600 $timer
# Tempfile for the script
shell=`mktemp $pathy/shell.XXXXXX` || exit 1
chmod 700 $shell
# Create email body: path, filename, and start time
starter=`date`
echo "$PWD/$1 finished" >> $timer
echo "$starter S" >> $timer
# Create script; add end time to email subject
echo "#!/bin/sh" >> $shell
echo "time -p $version -b do $1 " >> $shell
echo "date >> $timer" >> $shell
if [[ $email == "yes" ]]; then
echo "mutt -s \"$1 finished\" -a $output nohup.out -- [email protected] < $timer" >> $shell
fi
nohup $shell &
# Nohup takes a split second longer than other commands
sleep 0.01s