Sending Email when terminal is exit in which the golang application is runnin

Hi Team,

I am trying to write a code in which my golang application will send an email using ““github.com/jordan-wright/email”” this library .

Email will be sent when the terminal / iTERM in which the application is running is closed .

So here is what all info i was able to retrieve and wrote the following code.

Whenever terminal / iTerm is quit a SIGHUP signal is sent which if we handle can be used to do some operations.


package main
import (
    "fmt"
    "github.com/jordan-wright/email"
    "io/ioutil"
    "net/smtp"
    "os"
    "os/signal"
    "syscall"
)



func main() {


    c := make(chan os.Signal)
    done := make(chan bool)
    signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGHUP)

    go handleInterrupts(c,done)
    go execute1(done)
   
    //performing  some operation .......
}

func handleInterrupts(c chan os.Signal,done chan bool) {
    //signal := <-c
    signal := <-c
    fmt.Println("Signal is :=", signal.String())
    done <- true
}


func execute1(done chan bool) {
    <- done

    e := email.NewEmail()
    e.From = "Harshit Singhvi <harshitsinghvi@XXXXX.com>"
    e.To = []string{"harshit_singhvi@XXXX.com"}
    e.Subject = "Test Email"
    e.Text = []byte("Text Body is, of course, supported!")
    e.AttachFile("files/files.go")
    e.HTML = []byte("<h1>Demo</h1>")
    auth := smtp.PlainAuth("", "XXXXXX", "XXXXX", "XXXXX")
    e.Send("mail.XXXXX.com:25",auth)

    ioutil.WriteFile("output.log", []byte("Hello"), 0644)
    os.Exit(1)
}

the email gets send when a Ctrl + C is used to interrup the program

However whenever the terminal is closed SIGHUP is caught by the code but the email is never sent

Can anyone tell me how can i send an email when the terminal / iterm in which the above code is running is exited / terminated abruptly

When the parent process dies it will take its children with it. Some terminal emulators might give the shell some time to do clean shut down, others don’t.

Thanks for your reply @NobbZ

Can we have perform any operation for a certain duration before the child dies.

You can monitor the pid of the process but this is more a matter of devops than programming.

Did you try to run your app with nohup, disown or screen?

@samfrmd : i am running my application through simple go run command.

go run main.go

Please read about nohup , disown or screen. These will keep your app running over ctrl+c in terminal.

@samfrmd … my application is a menu driven application where user inout is required at different level hence i cannot run it through nohup as a background process .

Is there any other way to handle the closure of the parent process (terminal in this case)

hmm. Did you try defer in your exit part?

@samfrmd : i want a specific action to be executed only when the terminal is closed abruptly. Do you think defer would help here

No.

If the terminal is killed abprubtly, it depends on your terminal how your program will be killed, most terminals just kill their children as a kill -9 $pid would do. Your program has no chance to deal with those events.

1 Like

Your program is alive until it keeps running. Once it killed through system the program plus all children will be closed so there is nothing to handle your future functions.

hence there is no way to control the parent program in this case terminal in which the go program is running

Yep, it’s by system not your program.

As I says above, you can monitor your application with special designed tools for this. On Linux for example it’s a program named monit which can start and restart an application or a process, by a set of rules, without counting how or from where you started or why the process closed. You can also send emails on events and so on. But this is not a matter of programming but application administration.

Hi George, here monit cannot be used because i want to send email only when the terminal is closed abruptly when my program is running.

I don’t see the point, if the Terminal is closed the application is closed too. Monitoring any of them by file, pid or whatever can trigger an event and send a mail…

monit is used for applications which are required to be up and running . in this case the application will bee executed by the user only when required hence i don’t think its possible to configure monit for such scenario

Whys do you need that program to send an email on abprubt terminal close?

Why does it not need to send an email on a graceful shutdown or on loss of powersupply (or on recovery from it)?

this is just a question i had in mind and i m trying to code it out