Last active
October 17, 2020 09:31
-
-
Save jwolfson/d249afa9f470e76cb3e1 to your computer and use it in GitHub Desktop.
A Bash script for monitoring R simulations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Simulation running/monitoring script | |
#### | |
#### Run this as: ./runSim.sh YOUR_R_SOURCE_FILE.R ANY_TEXT_FILE.txt | |
#### | |
#### Lines to edit before running are indicated by '##### **' | |
RFILE="$1" | |
OUTFILE="$2" | |
nohup nice -10 R --vanilla --slave < $RFILE > $OUTFILE & ## Defaults to running at medium-low priority (-10). Can increase this to improve performance (maybe). | |
##### ** CHANGE THIS to your username on the server #### | |
USERNAME=myname | |
########################################## | |
##### ** CHANGE THIS to your email address #### | |
EMAIL="[email protected]" | |
############################################## | |
REPORTFILE="$OUTFILE" | |
PROCSOUT="/tmp/procs.txt" | |
GREPPROCS="/tmp/grepprocs.txt" | |
ps -u $USERNAME > $PROCSOUT ## Capture the list of running processes | |
grep "$RFILE" $PROCSOUT > $GREPPROCS ## Look for a process which contains the name of your source file. If this search comes up empty, your R session isn't running anymore and your simulation is done | |
while [ -s $GREPPROCS ] ## While your simulation is still running... | |
do | |
#echo "Still running" | |
sleep 100 ## Check again in 100 seconds | |
ps -u $USERNAME > $PROCSOUT | |
grep "$RFILE" $PROCSOUT > $GREPPROCS | |
done | |
SUBJECT="Auto-generated simulation message" | |
# Email text/message | |
EMAILMESSAGE="/tmp/emailmessage.txt" | |
echo "$RFILE" > $EMAILMESSAGE | |
echo "This simulation has terminated. Tail of reporting file follows:" >> $EMAILMESSAGE | |
tail -n 20 $REPORTFILE >>$EMAILMESSAGE | |
# send an email using /bin/mail | |
##### ** May have to CHANGE the path to /bin/mail here depending on your server configuration ######## | |
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE | |
################################################################################################# | |
cat $EMAILMESSAGE | |
echo "Script complete. Mail sent." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment