Skip to content

Instantly share code, notes, and snippets.

@jwolfson
Last active October 17, 2020 09:31
Show Gist options
  • Save jwolfson/d249afa9f470e76cb3e1 to your computer and use it in GitHub Desktop.
Save jwolfson/d249afa9f470e76cb3e1 to your computer and use it in GitHub Desktop.
A Bash script for monitoring R simulations
#### 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="whatever@whatever.com"
##############################################
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