# Mailredirect hook not working

**URL:** <https://community.openpbs.org/t/mailredirect-hook-not-working/1726>\
**Category:** Users/Site Administrators\
**Created:** [August 8, 2019, 2:17pm UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726 "2019-08-08T14:17:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![roemer2201](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@roemer2201](https://community.openpbs.org/u/roemer2201)\
**Post date:** [August 8, 2019, 2:17pm UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/1 "2019-08-08T14:17:59Z")

</div>

Dear All,

i like to have a hook (eventtype: queuejob) that sets the job attribute Mail\_Users to a default value if it the job was submitted without an email address. Please see the following code:

```
import pbs
import sys
try:
  e = pbs.event()
  j = e.job
  who = e.requestor

  mailusers = str(j.Mail_Users)
  user = str(who)
  if (mailusers == 'None'):
    j.Mail_Users = pbs.email_list(user.split('@')[0] + '@mailserver.example.com')
except SystemExit:
    pass
except:
    pbs.logmsg(pbs.EVENT_DEBUG2, "redirect_mail: exception %s" % '.'.join([repr(sys.exc_info([0]),repr(sys.exc_info()[1])]))
    e.accept()

```

If I print j.Mail\_Users to a file after it has been set, the new mail address is there. The problem is that it seems to not get pushed back to pbs. What am I doing wrong?  
Thank you for your help!

---

<div class="post-metadata">

**Author:** ![adarsh](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@adarsh](https://community.openpbs.org/u/adarsh)\
**Post date:** [August 9, 2019, 7:35am UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/2 "2019-08-09T07:35:40Z")

</div>

@roemer2201 I have tested the below code and it worked for me:

```
import pbs
import sys
e = pbs.event()
j = e.job
who = e.requestor
mailusers = str(j.Mail_Users)
pbs.logmsg(pbs.LOG_DEBUG, 'The mail users are %s' % str(mailusers))
user = str(who)
pbs.logmsg(pbs.LOG_DEBUG, 'The users is %s' % str(user))
if mailusers == "None":
    pbs.logmsg(pbs.LOG_DEBUG, 'I am here')
    j.Mail_Users = pbs.email_list('ilovepbspro@pbspro.org')
    e.accept()

```

Hope this help

---

<div class="post-metadata">

**Author:** ![roemer2201](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@roemer2201](https://community.openpbs.org/u/roemer2201)\
**Post date:** [August 9, 2019, 8:21am UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/3 "2019-08-09T08:21:49Z")

</div>

Thank you for your reply! Unfortunately it is not working on my side: According to the logs, the script does indeed set “j.Mail\_Users” correctly, but it does not apply to the job. I submit a testjob with

```
echo "sleep 300" | qsub -m abe

```

But the Mail\_Users attribute is not added to the job:

```
[user@login01 ~]$ qstat -f | grep Mail
    Mail_Points = abe

```

This results in an outgoing mail to “user@login01.cluster”. Can you please check, if your jobs get the Mail\_Users attribute after the hook has run?

I have added an extra Debug Line at the end:

```
mailusers2 = str(j.Mail_Users)
pbs.logmsg(pbs.LOG_DEBUG, 'The mail users are now %s' % str(mailusers2))

```

Server Logs:

```
08/09/2019 10:13:25;0086;Server@hpcc1;Svr;Server@hpcc1;Compiling script file: </var/spool/pbs/server_priv/hooks/redirect_mail.PY>
08/09/2019 10:13:25;0006;Server@hpcc1;Hook;Server@hpcc1;The mail users are None
08/09/2019 10:13:25;0006;Server@hpcc1;Hook;Server@hpcc1;The users is user
08/09/2019 10:13:25;0006;Server@hpcc1;Hook;Server@hpcc1;I am here
08/09/2019 10:13:25;0006;Server@hpcc1;Hook;Server@hpcc1;The mail users are now ilovepbspro@pbspro.org

```

Thank you in advance!

Regards

---

<div class="post-metadata">

**Author:** ![adarsh](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@adarsh](https://community.openpbs.org/u/adarsh)\
**Post date:** [August 9, 2019, 8:29am UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/4 "2019-08-09T08:29:29Z")

</div>

Yes i did test it and i can see this in my job details:

```
[root@pbsserver ~]# qstat -fx 12481 | grep -i mail
    Mail_Points = a
    Mail_Users = ilovepbspro@pbspro.org
	in,PBS_O_MAIL=/var/spool/mail/root,PBS_O_QUEUE=workq,
```

---

<div class="post-metadata">

**Author:** ![roemer2201](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@roemer2201](https://community.openpbs.org/u/roemer2201)\
**Post date:** [August 9, 2019, 8:35am UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/5 "2019-08-09T08:35:14Z")

</div>

I tried to implement the hook on a PBS v14.2.1 system and this shows the results above. I tried it again on PBS v19.2.2 and here it works. So what does PBS v14 do different?

---

<div class="post-metadata">

**Author:** ![roemer2201](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@roemer2201](https://community.openpbs.org/u/roemer2201)\
**Post date:** [August 9, 2019, 9:09am UTC](https://community.openpbs.org/t/mailredirect-hook-not-working/1726/6 "2019-08-09T09:09:32Z")

</div>

I don’t know, what was wrong, but deleting and recreating the hook helped, now its working on PBS v14

```
qmgr -c "del hook redirect_mail"
qmgr -c 'create hook redirect_mail event="queuejob"'
qmgr -c 'import hook redirect_mail application/x-python default redirect_mail.py'

```

Thank you for your help!
