Need script to send me a mail when calculation finishes...

Need script to send me a mail when calculation finishes...

am 04.04.2008 02:38:14 von megafedt

Dear all,

I'm using a linux pc which runs some large calculations that take about
1-2 days. I would like to use a script that every fifth minute checks if
the cpu-usage is above, say 30% (or whatever). If it aint above 30% I
assume that perhaps the calculation is done. Once done, I want the
script to send me an e-mail using standard linux commands. However, to
be absolutely sure the computation is done the cpu-usage for that
process should be above 30% twice in a row (at polls 2*5 minutes).

Is this possible? How? I'm pretty bad at scripting but know a few
basics. I was wondering about using "top" or similar. Ofcourse I would
need the program to automatically figure out the PID. But I guess I can
use some kind of "ps | grep -i "program_name" "...


Best regards
Martin Jørgensen

--
-----------------------------------------------
Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 04.04.2008 02:41:24 von megafedt

Martin Jørgensen wrote:
> Dear all,
>
> I'm using a linux pc which runs some large calculations that take about
> 1-2 days. I would like to use a script that every fifth minute checks if
> the cpu-usage is above, say 30% (or whatever). If it aint above 30% I
> assume that perhaps the calculation is done. Once done, I want the
> script to send me an e-mail using standard linux commands. However, to
> be absolutely sure the computation is done the cpu-usage for that
> process should be above 30% twice in a row (at polls 2*5 minutes).

Oops, .... should be _below_ 30% twice in a row, ofcourse... Then I
assume the computation is done...

> Is this possible? How? I'm pretty bad at scripting but know a few
> basics. I was wondering about using "top" or similar. Ofcourse I would
> need the program to automatically figure out the PID. But I guess I can
> use some kind of "ps | grep -i "program_name" "...

All script suggestions are welcome... I hope I don't need root access
(else I might talk to the sysadm). System: linux running redhat
(something - about 1-1,5 year old installation I think).


Best regards
Martin Jørgensen

--
-----------------------------------------------
Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 04.04.2008 02:51:10 von megafedt

Martin Jørgensen wrote:
> Martin Jørgensen wrote:
>> Dear all,
>>
>> I'm using a linux pc which runs some large calculations that take
>> about 1-2 days. I would like to use a script that every fifth minute
>> checks if the cpu-usage is above, say 30% (or whatever). If it aint
>> above 30% I assume that perhaps the calculation is done. Once done, I
>> want the script to send me an e-mail using standard linux commands.
>> However, to be absolutely sure the computation is done the cpu-usage
>> for that process should be above 30% twice in a row (at polls 2*5
>> minutes).
>
> Oops, .... should be _below_ 30% twice in a row, ofcourse... Then I
> assume the computation is done...
>
>> Is this possible? How? I'm pretty bad at scripting but know a few
>> basics. I was wondering about using "top" or similar. Ofcourse I would
>> need the program to automatically figure out the PID. But I guess I
>> can use some kind of "ps | grep -i "program_name" "...
>
> All script suggestions are welcome... I hope I don't need root access
> (else I might talk to the sysadm). System: linux running redhat
> (something - about 1-1,5 year old installation I think).

Erh, sorry. About the mail-thing:

I just googled a bit around and I think a mail-server should be
installed which I don't think is the case. However:

Is it possible to automate the following?

-------
$ ssh -X mylogin@myother_server.dk
(password entered)
(wait about 10 seconds)
$ mail my_email@address.com
Hi there, your computation using "program_name" is finished.
Thank you and goodbye.
..
(mail should now be sended)
$ exit
------

Server should now respond with:
logout
Connection to myother_server.dk closed.


I hope the above mail-thing is possible to automate....


Best regards
Martin Jørgensen

--
-----------------------------------------------
Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 04.04.2008 11:16:58 von Janis Papanagnou

On 4 Apr., 02:38, Martin J=F8rgensen wrote:
> Dear all,
>
> I'm using a linux pc which runs some large calculations that take about
> 1-2 days. I would like to use a script that every fifth minute checks if
> the cpu-usage is above, say 30% (or whatever). If it aint above 30% I
> assume that perhaps the calculation is done. Once done, I want the
> script to send me an e-mail using standard linux commands. However, to
> be absolutely sure the computation is done the cpu-usage for that
> process should be above 30% twice in a row (at polls 2*5 minutes).

Polling is a bad approach and usually unnecessary. Moreover the
CPU load doesn't really tell you anything in any reliable way.

>
> Is this possible? How? I'm pretty bad at scripting but know a few
> basics. I was wondering about using "top" or similar. Ofcourse I would
> need the program to automatically figure out the PID. But I guess I can
> use some kind of "ps | grep -i "program_name" "...

It's of course possible to intercept the PID and check that.

But given the small time scale you posted above I suppose you
don't need that to be informed once about the actual job but
rather for future invocations? If that is the case you may try

program_name program_args ; mailx -s finished you@your.domain

You can provide a text body to the mail command through standard
input. Or even pipe the whole output of your program to the mail
command (beware if the output is huge)

program_name program_args 2>&1 | mailx -s ...

If you want your prompt back after invocation start that program
sequence within a subshell as a background process

( ... )&

In case you insist on polling because your program is already
running something like the following can be done

while ps ... | grep -s known_pid
do sleep 600 # 5 minutes
done

But mind that you typically cannot rely on the ps output format;
see whether it supports option -o to make the grep more reliable.

Janis

>
> Best regards
> Martin J=F8rgensen
>
> --
> -----------------------------------------------
> Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 05.04.2008 00:33:58 von megafedt

Janis wrote:
> On 4 Apr., 02:38, Martin Jørgensen wrote:
>> Dear all,
>>
>> I'm using a linux pc which runs some large calculations that take about
>> 1-2 days. I would like to use a script that every fifth minute checks if
>> the cpu-usage is above, say 30% (or whatever). If it aint above 30% I
>> assume that perhaps the calculation is done. Once done, I want the
>> script to send me an e-mail using standard linux commands. However, to
>> be absolutely sure the computation is done the cpu-usage for that
>> process should be above 30% twice in a row (at polls 2*5 minutes).
>
> Polling is a bad approach and usually unnecessary. Moreover the
> CPU load doesn't really tell you anything in any reliable way.

What is a better alternative?

>> Is this possible? How? I'm pretty bad at scripting but know a few
>> basics. I was wondering about using "top" or similar. Ofcourse I would
>> need the program to automatically figure out the PID. But I guess I can
>> use some kind of "ps | grep -i "program_name" "...
>
> It's of course possible to intercept the PID and check that.
>
> But given the small time scale you posted above I suppose you
> don't need that to be informed once about the actual job but
> rather for future invocations? If that is the case you may try
>
> program_name program_args ; mailx -s finished you@your.domain

Sorry, that doesn't work.

The computation is started from inside a GUI and I cannot make the
program exit automatically after the simulation is done....

Also: I believe "mailx" requires a mail-server to be installed? I added
something about ssh'ing in my last posting, AFAIR.

> You can provide a text body to the mail command through standard
> input. Or even pipe the whole output of your program to the mail
> command (beware if the output is huge)
>
> program_name program_args 2>&1 | mailx -s ...

Thank you, but that doesn't work for me since the simulation is started
from a GUI and the simulation program is not text/console-based...

> If you want your prompt back after invocation start that program
> sequence within a subshell as a background process
>
> ( ... )&
>
> In case you insist on polling because your program is already
> running something like the following can be done
>
> while ps ... | grep -s known_pid
> do sleep 600 # 5 minutes
> done

I'm not really sure I understand what to do with this. What do I insert
at the 3 dots?

I need to check when the CPU falls below 30% twice in a row, as I wrote.
You're assuming my simulation program is text/console-based, which it isn't.

The process will not kill itself after simulation is done.

> But mind that you typically cannot rely on the ps output format;
> see whether it supports option -o to make the grep more reliable.

Well, thank you for trying even though I really think you misunderstood
the question...

Again: Since the simulations are started from a GUI I don't believe I
can use most of your suggestions...

I welcome any other suggestions anyone has... I hope somebody here knows
how to solve the problem and wants to share the knowledge about how to
do it.



Best regards
Martin Jørgensen

--
-----------------------------------------------
Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 05.04.2008 06:29:01 von Dan Stromberg

On Fri, 04 Apr 2008 02:51:10 +0200, Martin Jørgensen wrote:

> Martin Jørgensen wrote:
>> Martin Jørgensen wrote:
>>> Dear all,
>>>
>>> I'm using a linux pc which runs some large calculations that take
>>> about 1-2 days. I would like to use a script that every fifth minute
>>> checks if the cpu-usage is above, say 30% (or whatever). If it aint
>>> above 30% I assume that perhaps the calculation is done. Once done, I
>>> want the script to send me an e-mail using standard linux commands.
>>> However, to be absolutely sure the computation is done the cpu-usage
>>> for that process should be above 30% twice in a row (at polls 2*5
>>> minutes).
>>
>> Oops, .... should be _below_ 30% twice in a row, ofcourse... Then I
>> assume the computation is done...
>>
>>> Is this possible? How? I'm pretty bad at scripting but know a few
>>> basics. I was wondering about using "top" or similar. Ofcourse I would
>>> need the program to automatically figure out the PID. But I guess I
>>> can use some kind of "ps | grep -i "program_name" "...
>>
>> All script suggestions are welcome... I hope I don't need root access
>> (else I might talk to the sysadm). System: linux running redhat
>> (something - about 1-1,5 year old installation I think).
>
> Erh, sorry. About the mail-thing:
>
> I just googled a bit around and I think a mail-server should be
> installed which I don't think is the case. However:
>
> Is it possible to automate the following?
>
> -------
> $ ssh -X mylogin@myother_server.dk
> (password entered)
> (wait about 10 seconds)
> $ mail my_email@address.com
> Hi there, your computation using "program_name" is finished. Thank you
> and goodbye.
> .
> (mail should now be sended)
> $ exit
> ------
>
> Server should now respond with:
> logout
> Connection to myother_server.dk closed.
>
>
> I hope the above mail-thing is possible to automate....
>
>
> Best regards
> Martin Jørgensen

It's a simple program, but I use it a lot so I can run off and work on
task n+1 while as I'm waiting for tasks 1..n to finish:

http://stromberg.dnsalias.org/~strombrg/notify-when-up2.html

It'll do the e-mail, give you a little X11 popup window, and handle the
ssh stuff for you if you set up passwordless authentication:

http://stromberg.dnsalias.org/~strombrg/ssh-keys.html

Re: Need script to send me a mail when calculation finishes...

am 05.04.2008 06:46:53 von Dan Stromberg

On Sat, 05 Apr 2008 00:33:58 +0200, Martin Jørgensen wrote:

> Janis wrote:
>> On 4 Apr., 02:38, Martin Jørgensen wrote:
>>> Dear all,
>>>
>>> I'm using a linux pc which runs some large calculations that take
>>> about 1-2 days. I would like to use a script that every fifth minute
>>> checks if the cpu-usage is above, say 30% (or whatever). If it aint
>>> above 30% I assume that perhaps the calculation is done. Once done, I
>>> want the script to send me an e-mail using standard linux commands.
>>> However, to be absolutely sure the computation is done the cpu-usage
>>> for that process should be above 30% twice in a row (at polls 2*5
>>> minutes).
>>
>> Polling is a bad approach and usually unnecessary. Moreover the CPU
>> load doesn't really tell you anything in any reliable way.
>
> What is a better alternative?
>
>>> Is this possible? How? I'm pretty bad at scripting but know a few
>>> basics. I was wondering about using "top" or similar. Ofcourse I would
>>> need the program to automatically figure out the PID. But I guess I
>>> can use some kind of "ps | grep -i "program_name" "...
>>
>> It's of course possible to intercept the PID and check that.
>>
>> But given the small time scale you posted above I suppose you don't
>> need that to be informed once about the actual job but rather for
>> future invocations? If that is the case you may try
>>
>> program_name program_args ; mailx -s finished you@your.domain
>
> Sorry, that doesn't work.
>
> The computation is started from inside a GUI and I cannot make the
> program exit automatically after the simulation is done....
>
> Also: I believe "mailx" requires a mail-server to be installed? I added
> something about ssh'ing in my last posting, AFAIR.
>
>> You can provide a text body to the mail command through standard input.
>> Or even pipe the whole output of your program to the mail command
>> (beware if the output is huge)
>>
>> program_name program_args 2>&1 | mailx -s ...
>
> Thank you, but that doesn't work for me since the simulation is started
> from a GUI and the simulation program is not text/console-based...
>
>> If you want your prompt back after invocation start that program
>> sequence within a subshell as a background process
>>
>> ( ... )&
>>
>> In case you insist on polling because your program is already running
>> something like the following can be done
>>
>> while ps ... | grep -s known_pid
>> do sleep 600 # 5 minutes
>> done
>
> I'm not really sure I understand what to do with this. What do I insert
> at the 3 dots?
>
> I need to check when the CPU falls below 30% twice in a row, as I wrote.
> You're assuming my simulation program is text/console-based, which it
> isn't.
>
> The process will not kill itself after simulation is done.
>
>> But mind that you typically cannot rely on the ps output format; see
>> whether it supports option -o to make the grep more reliable.
>
> Well, thank you for trying even though I really think you misunderstood
> the question...
>
> Again: Since the simulations are started from a GUI I don't believe I
> can use most of your suggestions...
>
> I welcome any other suggestions anyone has... I hope somebody here knows
> how to solve the problem and wants to share the knowledge about how to
> do it.
>
>
>
> Best regards
> Martin Jørgensen

This being a simulation that doesn't have a pid that disappears at the
end, you're probably best off checking how much CPU time is being
accumulated (whether by ps or top), or perhaps using strace and counting
syscalls.

I have lots of notify-when-up (the old version of the program which
doesn't do ssh for you) examples at http://stromberg.dnsalias.org/
~strombrg/notify-when-up.html including checking for when something drops
out of top for a while. The usage is mostly the same between notify-when-
up and notify-when-up2.

Machine polling isn't great typically, but it's generally better than
human polling :) notify-when-up2 and notify-when-up are both pollers
with most of their options. I used to avoid machine polling on some sort
of principle, but my life got more productive when I got over it.

Re: Need script to send me a mail when calculation finishes...

am 06.04.2008 20:38:27 von megafedt

Dan Stromberg wrote:
> On Sat, 05 Apr 2008 00:33:58 +0200, Martin Jørgensen wrote:
>
>> Janis wrote:
>>> On 4 Apr., 02:38, Martin Jørgensen wrote:
>>>> Dear all,
>>>>
>>>> I'm using a linux pc which runs some large calculations that take
>>>> about 1-2 days. I would like to use a script that every fifth minute
>>>> checks if the cpu-usage is above, say 30% (or whatever). If it aint
>>>> above 30% I assume that perhaps the calculation is done. Once done, I
>>>> want the script to send me an e-mail using standard linux commands.
>>>> However, to be absolutely sure the computation is done the cpu-usage
>>>> for that process should be above 30% twice in a row (at polls 2*5
>>>> minutes).
>>> Polling is a bad approach and usually unnecessary. Moreover the CPU
>>> load doesn't really tell you anything in any reliable way.
>> What is a better alternative?
>>
>>>> Is this possible? How? I'm pretty bad at scripting but know a few
>>>> basics. I was wondering about using "top" or similar. Ofcourse I would
>>>> need the program to automatically figure out the PID. But I guess I
>>>> can use some kind of "ps | grep -i "program_name" "...
>>> It's of course possible to intercept the PID and check that.
>>>
>>> But given the small time scale you posted above I suppose you don't
>>> need that to be informed once about the actual job but rather for
>>> future invocations? If that is the case you may try
>>>
>>> program_name program_args ; mailx -s finished you@your.domain
>> Sorry, that doesn't work.
>>
>> The computation is started from inside a GUI and I cannot make the
>> program exit automatically after the simulation is done....
>>
>> Also: I believe "mailx" requires a mail-server to be installed? I added
>> something about ssh'ing in my last posting, AFAIR.
>>
>>> You can provide a text body to the mail command through standard input.
>>> Or even pipe the whole output of your program to the mail command
>>> (beware if the output is huge)
>>>
>>> program_name program_args 2>&1 | mailx -s ...
>> Thank you, but that doesn't work for me since the simulation is started
>> from a GUI and the simulation program is not text/console-based...
>>
>>> If you want your prompt back after invocation start that program
>>> sequence within a subshell as a background process
>>>
>>> ( ... )&
>>>
>>> In case you insist on polling because your program is already running
>>> something like the following can be done
>>>
>>> while ps ... | grep -s known_pid
>>> do sleep 600 # 5 minutes
>>> done
>> I'm not really sure I understand what to do with this. What do I insert
>> at the 3 dots?
>>
>> I need to check when the CPU falls below 30% twice in a row, as I wrote.
>> You're assuming my simulation program is text/console-based, which it
>> isn't.
>>
>> The process will not kill itself after simulation is done.
>>
>>> But mind that you typically cannot rely on the ps output format; see
>>> whether it supports option -o to make the grep more reliable.
>> Well, thank you for trying even though I really think you misunderstood
>> the question...
>>
>> Again: Since the simulations are started from a GUI I don't believe I
>> can use most of your suggestions...
>>
>> I welcome any other suggestions anyone has... I hope somebody here knows
>> how to solve the problem and wants to share the knowledge about how to
>> do it.
>>
>>
>>
>> Best regards
>> Martin Jørgensen
>
> This being a simulation that doesn't have a pid that disappears at the
> end, you're probably best off checking how much CPU time is being
> accumulated (whether by ps or top), or perhaps using strace and counting
> syscalls.
>
> I have lots of notify-when-up (the old version of the program which
> doesn't do ssh for you) examples at http://stromberg.dnsalias.org/
> ~strombrg/notify-when-up.html including checking for when something drops
> out of top for a while. The usage is mostly the same between notify-when-
> up and notify-when-up2.
>
> Machine polling isn't great typically, but it's generally better than
> human polling :) notify-when-up2 and notify-when-up are both pollers
> with most of their options. I used to avoid machine polling on some sort
> of principle, but my life got more productive when I got over it.

Ok, thank you. I'll try to look at the pages you posted links to.


Best regards
Martin Jørgensen

--
-----------------------------------------------
Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 07.04.2008 11:47:46 von Janis Papanagnou

On 5 Apr., 00:33, Martin J=F8rgensen wrote:
> Janis wrote:
> > On 4 Apr., 02:38, Martin J=F8rgensen wrote:
> >> Dear all,
>
> >> I'm using a linux pc which runs some large calculations that take about=

> >> 1-2 days. I would like to use a script that every fifth minute checks i=
f
> >> the cpu-usage is above, say 30% (or whatever). If it aint above 30% I
> >> assume that perhaps the calculation is done. Once done, I want the
> >> script to send me an e-mail using standard linux commands. However, to
> >> be absolutely sure the computation is done the cpu-usage for that
> >> process should be above 30% twice in a row (at polls 2*5 minutes).
>
> > Polling is a bad approach and usually unnecessary. Moreover the
> > CPU load doesn't really tell you anything in any reliable way.
>
> What is a better alternative?

The one I explained.

>
> >> Is this possible? How? I'm pretty bad at scripting but know a few
> >> basics. I was wondering about using "top" or similar. Ofcourse I would
> >> need the program to automatically figure out the PID. But I guess I can=

> >> use some kind of "ps | grep -i "program_name" "...
>
> > It's of course possible to intercept the PID and check that.
>
> > But given the small time scale you posted above I suppose you
> > don't need that to be informed once about the actual job but
> > rather for future invocations? If that is the case you may try
>
> > =A0 program_name program_args ; mailx -s finished y...@your.domain
>
> Sorry, that doesn't work.
>
> The computation is started from inside a GUI and I cannot make the
> program exit automatically after the simulation is done....

Want to explain that? Is "the computation" an own external program
started from the GUI? Then write a wrapper script around that program.
Is "the computation" a function of a GUI based program? Then start
the GUI based program from command line the way I showed.

>
> Also: I believe "mailx" requires a mail-server to be installed? I added
> something about ssh'ing in my last posting, AFAIR.

Hmm.. - I've not seen a Linux box without programs mail or mailx
installed, and sendmail running. Sorry to hear that. But then, how
do you think your requirement to send a mail can be be fulfilled at
all if you have no mail system available?

>
> > You can provide a text body to the mail command through standard
> > input. Or even pipe the whole output of your program to the mail
> > command (beware if the output is huge)
>
> > =A0 program_name program_args 2>&1 | mailx -s ...
>
> Thank you, but that doesn't work for me since the simulation is started
> from a GUI and the simulation program is not text/console-based...

See above.

>
> > If you want your prompt back after invocation start that program
> > sequence within a subshell as a background process
>
> > =A0 ( ... )&
>
> > In case you insist on polling because your program is already
> > running something like the following can be done
>
> > =A0 while =A0ps ... | grep -s known_pid
> > =A0 do =A0sleep 600 =A0# 5 minutes
> > =A0 done
>
> I'm not really sure I understand what to do with this. What do I insert
> at the 3 dots?

The ps options that fit to the ps your system provides you (I had
given an example at bottom of my posting).

>
> I need to check when the CPU falls below 30% twice in a row, as I wrote.
> You're assuming my simulation program is text/console-based, which it isn'=
t.

No, I haven't assumed that (see above).

>
> The process will not kill itself after simulation is done.

Okay, that is new information. Leaves still two possibilities; does
it invoke external programs that you want to monitor (then see above),
or has is all as built-in functions (which might explain your demand
for top)?

>
> > But mind that you typically cannot rely on the ps output format;
> > see whether it supports option -o to make the grep more reliable.
>
> Well, thank you for trying even though I really think you misunderstood
> the question...

No, I was just trying to provide you with solutions that could have
been more reliable than what you thought of. Sorry that they don't
match your requirements.

>
> Again: Since the simulations are started from a GUI I don't believe I
> can use most of your suggestions...
>
> I welcome any other suggestions anyone has... I hope somebody here knows
> how to solve the problem and wants to share the knowledge about how to
> do it.
>
> Best regards
> Martin J=F8rgensen
>
> --
> -----------------------------------------------
> Hi, there. I'm just another signature motherfucker.

Re: Need script to send me a mail when calculation finishes...

am 07.04.2008 18:14:10 von Maxwell Lol

Janis writes:

> Hmm.. - I've not seen a Linux box without programs mail or mailx
> installed, and sendmail running. Sorry to hear that. But then, how
> do you think your requirement to send a mail can be be fulfilled at
> all if you have no mail system available?


Well, you can telnet to the SMTP port and send text.

Re: Need script to send me a mail when calculation finishes...

am 08.04.2008 13:13:17 von Janis Papanagnou

On 7 Apr., 18:14, Maxwell Lol wrote:
> Janis writes:
> > Hmm.. - I've not seen a Linux box without programs mail or mailx
> > installed, and sendmail running. Sorry to hear that. But then, how
> > do you think your requirement to send a mail can be be fulfilled at
> > all if you have no mail system available?
>
> Well, you can telnet to the SMTP port and send text.

Good point. (I often forget about that; it's just so rare
that basic programs are missing on a system which makes it
necesary to resort to a lower-level generic telnet solution.)

Janis