The following is an example of how environment file commands can be used to notify Linux system users when their job is finished. The notification method used depends on how the job was run and if the user is logged in. If the job was run interactively, the user will not be notified that the job has finished. If the user is still logged in when the job completes, a message will be output to the screen. If the user has logged out by the time the job completes, a message will be mailed to the user. The syntax of the mail command varies from system to system. Please consult your system documentation to determine the appropriate commands.
def onJobCompletion():
import os, re
userName = os.environ['USER']
msg = 'Job %s has completed' % id
# Run "who" command, pipe the output, and read into a list
whopipe = os.popen('who', 'r')
output = whopipe.readlines()
whopipe.close()
# Find out if the user is logged in
loggedIn = 'no'
terminal = [ ]
for line in output:
columns = re.split('[ ]+', line) # Split into blank separated columns
name = columns[0] # User name is in the first column
if name == userName:
terminal.append(columns[1]) # Terminal at which user is logged in
loggedIn = 'yes'
# Use "write" command if the user is logged in, use mail otherwise
if loggedIn == 'no':
logFile = savedir + id + ".log"
if os.path.exists(logFile):
os.system('cat %s | Mail -s "%s" %s' % (logFile, msg, userName))
else:
os.system('Mail -s "%s" %s' % (msg, userName))
else:
for termNum in terminal:
os.system('echo "%s" | write %s %s' % (msg, userName, termNum))