conditionally sending cron job emails - perl

I'm not even sure if what I want is possible, but I'd like to run a Cron job where an email is only sent in certain conditions. I know that you can prevent mail from being sent at all by setting MAILTO to an empty string in the crontab file, but I've searched in several different ways, and can't find anything about sending email conditionally. My end goal is to run a Cron job that periodically checks whether the webserver is running, and if not, restart it. I only want an email if the webserver has to be restarted. I'm writing my Cron jobs in Perl. Is there a Perl command I can use within the job script that will disable the email in certain cases? Thanks for any help you can give me.

Cronjobs will send emails if the command you are running generate output. If you write your script to only send output to STDERR/STDOUT when you want an email, that should accomplish your goal.

There are 2 possibilities to send mails from cron jobs:
From program, that has been started by cron daemon,
From UNIX/Linux mechanism, that can send mail, if a program, that has been started as a cron job, has written something to STDOUT or STDERR.
I don't recommend to use the 2nd possibility. It is inflexible. You can't send mails to different recipients, depending on what alert has happened.
Usage of the 2nd way is rather a bad design. Cron jobs should redirect all their stdout and stderr to an idividual for every cron job log file for possible troubleshooting.
Perl possesses perfect possibilities to send mails, e.g. using MIME::Lite module.
This module is not a core one, so that you might should request sysadmin to install this module, if it's not available.
If you will use the 1st way, then your issue is easy to solve using Perl logic: just send the required mail from your Perl program after this program restarted the web server.

Related

Not able to send automated mails by task scheduler and python

I use a python script to send mails. The script runs a VBA macro.
I'm scheduling the python script to run at a particular time.
Configuration
My trust settings in Outlook has all macros enabled.
I'm running the task with highest priority too.
Problem
I am able to send mails if I run the script manually.
However, when I try to run it via Windows Task Scheduler, mails are not being sent.
Try a different scheduler and see if this works.
For example Splinterware system scheduler.
Trying a different scheduler can help you figure out if the problem is
a setting in the scheduler (like not having the password entered)
or in your Python script (which would affect all schedulers)
There is a way to send emails from Outlook automatically without the use of Task Scheduler. It is outlined neatly here.
It uses recurring reminders, so Outlook must be kept running at all times (but the macro is only executed when the reminder fires).

Sending test result mail through jmeter

I have incorporated a simple HTTP Request sampler in a test plan. And, viewing the result through "View Result Tree" and writing the same results to a file. I want this file to send through mail Sampler.
Issue is, once I execute the test-case, it sends mail first and then append new test results to the file. So, in mail I got the previous run results instead of the new one.
How can I incorporate this?
Reference Article - Send JMeter Test Reports Automatically in Email
If you use 'View Results Tree' or 'Summary Report' Listeners, you won't be able to send the current report, through mail via SMTP Sampler, even if you use tearDown Thread Group.
Reason : JMeter writes the data to these report files, only after the test is finished completely. You can verify this by adding a tearDown Thread Group and adding a delay before running the SMTP Sampler. Keep an eye on the file size, it is created once the test is started, but data is written when the last thread (SMTP Sampler, in this case) is finished execution.
Hence, only an empty file would be attached with your email.
Solution -
1. Find and modify (or add, if not present) the following section in the 'user.properties' file, setting the autoflush parameter to true:
# AutoFlush on each line written in XML or CSV output
# Setting this to true will result in less test results data loss in case of Crash
# but with impact on performances, particularly for intensive tests (low or no pauses)
# Since JMeter 2.10, this is false by default
jmeter.save.saveservice.autoflush=true
Use a batch script, and send the mail using some other utility after your JMeter Test, as suggested by Ashish.
Problem with this approach is, there is a dependency on another utility. Also, we would require to write different scripts for different platforms (Windows/Linux).
Instead of using default in-built JMeter plugins, we can use jp#gc JMeter Plugins, which provides very powerful reporting elements.
In this case, what we need is a 'Flexible File Writer' Plugin. It provides you powerful customization to configure what data you want to write to your report file, and also it would allow you to include complete report file to be sent via SMTP Sampler.
I was able to achieve this using Flexible File Writer Listener, and tearDown Thread Group. Here is the structure of my Test Plan -
Test Plan
--- Thread Group
-------Sampler
-------jp#gc- Flexible Filw Writer
---tearDown Thread Group
------SMTP Sampler
Here is a snapshot of my File Writer Plugin's configuration :
You may want to add some delay before sending the mail from tearDown Thread Group.
You can download the Flexible File Writer plugin using in-built Jmeter's Plugin Manager, or from JMeter Plugins Site - jp#gc Flexible File Writer Plugin
I observe that JMeter first sends the Email with either blank or old run report when doing it using the SMTP Sampler.
To overvome it, this is what I tried:
I created a batch file to call the Jmeter from command line as:
call jmeter -n -t "script path" -l "JTL report path"
I used a utility called sendEmail which is available in below location:
http://caspian.dotconf.net/menu/Software/SendEmail/
as:
sendEmail -f Sender_Email -t Receiver_Email -cc CC_Email -s Mail_Server -u "Subject" -m "Email body" -a "Path of JTL Report attachment"
What it is doing that it executes the JMeter script, generate the report in path specified in first step and then send Email with the report using step 2.
It might help.
I would suggest sending your email in tearDown Thread Group which is designed for post processing purposes and seems to be exactly what you're looking for.
Also make sure that you wait for reasonable amount of time via Test Action sampler as JMeter doesn't update results file on the fly, it periodically flushes chunks of results.
If you don't want "old" data you can use setUp Thread Group with i.e. OS Process Sampler to delete old results file or move it somewhere else.
For more JMeter email tips and tricks see Load Testing Your Email Server: How to Send and Receive E-mails with JMeter

Is it possible to send a email through Batch file

Is it possible to send an email through a batch command line? I have a password batch file for a folder and when you fail three attempts i want it to send me an email. I know its possible to download ans install a program off the internet but when I would open the program it would close. So is there a command or program that sends and email through batch?
TakeCommand will enable all kinds of commandline-magic, including sending of mail. Alkternatively, if you have a local install of PHP (and properly set it up), you could write a PHP-Script to send mail and launch the PHP-Interpreter (in your batch) to execute that file.

KSH script won't email when nohup

I have a unique issue, i am in a unix environment and have a ksh script that ssh's to multiple sites, executes some code and then returns a response and then emails that response to an email address.
The script works perfectly when i run it, but since it must run for several hours i wish to nohup the script.
Here is where the problem is. When i nohup the script the email is not sent. I have scoured the boards looking for a reason or solution to no avail. if someone could point me in the right direction i would greatly appreciate it.
Here is my mail portion of the script:
mail -s "subject" email#address.com < /usr/etc/bin/mydir/infofile.out &&
rm -f infofile.out
exit;
EDIT: my environment is AIX 6.1.7.1
Finally figured out the answer, and even thou i was being dumb, i feel i have a responsibility to answer anyway, just in case someone else runs across this issue.
Turns out when i nohup my script it DOES send the email correctly. Its just that by nohuping and logging out it forces the email to be sent from the unix mail utility's default email address, and in my environment that address sends out hundreds of useless alerts, most of which i have filtered in outlook to go to a trash folder, well the email i was sending ended up in that trash folder.
Thanks to those who responded, especially shellter, your recommendation to use shell debugging is what let me know that it was sending from that default mail account.

Tailing 'Jobs' with Perl under mod_perl

I've got this project running under mod_perl shows some information on a host. On this page is a text box with a dropdown that allows users to ping/nslookup/traceroute the host. The output is shown in the text box like a tail -f.
It works great under CGI. When the user requests a ping it would make an AJAX call to the server, where it essentially starts the ping with the output going to a temp file. Then subsequent ajax calls would 'tail' the file so that the output was updated until the ping finished. Once the job finished, the temp file would be removed.
However, under mod_perl no matter what I do I can's stop it from creating zombie processes. I've tried everything, double forking, using IPC::Run etc. In the end, system calls are not encouraged under mod_perl.
So my question is, maybe there's a better way to do this? Is there a CPAN module available for creating command line jobs and tailing output that will work under mod_perl? I'm just looking for some suggestions.
I know I could probably create some sort of 'job' daemon that I signal with details and get updates from. It would run the commands and keep track of their status etc. But is there a simpler way?
Thanks in advance.
I had a short timeframe on this one and had no luck with CPAN, so I'll provide my solution here (I probably re-invented the wheel). I had to get something done right away.
I'll use ping in this example.
When ping is requested by the user, the AJAX script creates a record in a database with the details of the ping (host, interval, count etc.). The record has an auto-incrementing ID field. It then sends a SIGHUP to to a job daemon, which is just a daemonised perl script.
This job daemon receives the SIGHUP, looks for new jobs in the database and processes each one. When it gets a new job, it forks, writes the PID and 'running' status to the DB record, opens up stdout/stderr files based on the unique job ID and uses IPC::Run to direct STDOUT/STDERR to these files.
The job daemon keeps track of the forked jobs, killing them if they run too long etc.
To tail the output, the AJAX script send back the job ID to the browser. Then on a Javascript timer, the AJAX script is called which basically checks the status of the job via the database record and tails the files.
When the ping finishes, the job daemon sets the record status to 'done'. The AJAX script checks for this on it's regular status checks.
One of the reasons I did it this way is that the AJAX script and the job daemon talk through and authenticated means (the DB).