shell脚本退出前发送电子邮件脚本、发送电子邮件、shell

2023-09-11 09:39:18 作者:一直都是笑话

我试图写一个shell脚本,将退出本身之前给我发电子邮件

i am trying to write a shell script that will send me an email before exiting itself

由于我在Amazon EC2上的Debain点要求运行这一点,就应该给我一个电子邮件的情况下终止之前

as i am running this on an amazon ec2 DEBAIN spot request and it should send me an email before the instances is terminated

            #!/bin/bash
            sendEmailLog()
            {
            emailLog=true
               if [ $emailLog = "true" ];then
                    /usr/bin/mpack -s "emailed log" -a "/srv/log.html" "emailid@gmail.com";
               fi
               exit 0;
            }

            trap sendEmailLog INT TERM EXIT KILL QUIT

            aws ec2 terminate-instances instances ID

我想上面的code是亘古不变的终止之前给我发电子邮件 我使用的Debain 7

i tried the above code it doesnot send me an email before its terminated i am using DEBAIN 7

推荐答案

我想你可能有一对夫妇的问题。我不是超级熟悉MPack,还将,但它看起来像你的语法是正确的(除了;结尾)。做一些修改,下面,并看看是否有帮助。

I think you might have a couple issues. I'm not super-familiar with mpack, but it looks like your syntax is correct (except the ";" at the end). Make a couple changes, below, and see if it helps.

我会用引号定义emailLog字符串,虽然它不是真的不清楚为什么你使用一个字符串的布尔语句,据我所知,你要始终运行。除非你要得到一个错误code。

I would use quotes define your emailLog string, although it's not really clear why you're using a string for a boolean statement that, as far as I can tell, you want to ALWAYS run. Unless you want to get an error code.

 emailLog="true"

要检查是否相等,用两个==:

To check for equality, use two ==:

if [ $emailLog == "true" ]; then

和我说过,';'是多余的。

And as I said, the ';' is superfluous.

如果我要重新写吧,我会写这样的:

If I were to re-write it, I would write it like this:

sendEmailLog() {
    /usr/bin/mpack -s "emailed log" -a "/srv/log.html" "emailid@gmail.com" && exit 1
    exit 0
}

trap sendEmailLog SIGINT SIGTERM SIGKILL

aws ec2 terminate-instances instances ID