5

My upstart config looks something like this

start on filesystem
stop on runlevel S

respawn

exec /path/to/my/script

When this script dies and is respawned I would like to receive an email. Is this possible, or am I using the wrong tool for the job?

Thanks!

mloughran
  • 151
  • 3

3 Answers3

2

You could write a post-start stanza to send e-mail(s).

# An Upstart script to manage the foo service
respawn

post-start script
    echo "my-foo service started at `date +"%F %T.%N"`" | mail -s "My-foo Service Started" me@mydomain.org
    echo "my-foo service started at `date +"%F %T"`" | mail -s "My-foo Service Started" them@theirdomain.org
end script

exec -u foo-user /usr/local/bin/foo
Jacob Wan
  • 171
  • 1
  • 4
0

You could add a mail command inside your script or write a wrapper script that does that and then calls your script. Then you'd have Upstart use your wrapper script.

echo 'Script scriptname respawned' | mail -s "respawn notice" foo@example.com
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
0

Write another job that will start on the stopped event for the job you are monitoring. Inside this job you can check EXIT_CODE and take action accordingly, e.g. :

# /etc/init/service_alert.conf
start on stopped myservice

task

script
    env >> /var/log/service_alert.log
    [ $EXIT_STATUS -ne 0 ] && echo "Env: $(env)" | mail -s "Alert $JOB is down!" page@example.com 
end script
rags
  • 328
  • 1
  • 2
  • 6