I have one cron job like this:
* * * * * urlwatch | mail -s "job changes" pc_xxx#msn.com
It mails every minute as expected. However when I alter the test html page on my local server it doesn't email the differences, just continues to mail a blank mail with the title 'job changes'.
When I paste the job to a prompt:
pc#dellbox:/$urlwatch | mail -s "job changes" pc_xxx#msn.com
and run before/after changing the html, it emails the differences in the 2nd email as expected.
(pc is the owner of urls.txt and the cronjob was created by pc via crontab -e)
Why does the cron version not email the urlwatch output?
This is driving me batty...
Any/all help gratefully received.
ps couldn't create urlwatch as a new tag - need 1500 rep :(
Update:
If I split the command into two bits like this:
urlwatch > ~/.urlwatch/output.txt
mail -s "output" pc_xxx#msn.com < ~/.urlwatch/output.txt
This works.
If I join the two statements with a pipe like this:
urlwatch > ~/.urlwatch/output.txt | mail -s "output" pc_xxx#msn.com < ~/.urlwatch/output.txt
I get a prompt IMMEDIATELY that says
Null message body; hope that's ok
I notice urlwatch takes 2 - 3 seconds to complete, and I understand shell commands wait for preceding commands to finish (unless you're using &?). Dunno if this is significant.
Also, I'm using sSMTP...
Use full path when calling urlwatch:
$ which urlwatch
/usr/bin/urlwatch
Then your cron have to be::
* * * * * /usr/bin/urlwatch | mail -s "job changes" pc_xxx#msn.com
Put .urlwatch directories under /root/ as the program will look there to store its data
#askchipbug: your case looks very specific, but here are the things to do that may help:
-check /var/log/mail.err or /var/log/mail.log to find hints
-crontab user/permission
-some server strips content for not a good reason
-i use mailx it works fine for me and here is my cron job in crontab -e:
* * * * * urlwatch --urls=/var/www/html/urls.txt --hooks=/home/foo/hooks.py | mailx -v -r "bar#gmail.com" -s "This is the subject line" -S smtp="smtp.gmail.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="foo2#gmail.com" -S smtp-auth-password="bardaf16charsfoo" -S ssl-verify=ignore receipient#hotmail.com
i found the info for heirloom-mailx client installation from here: http://www.binarytides.com/linux-mail-with-smtp/
hope it helps.
Related
I want not to save the logs that are "warning" in the log file that the crontab creates, I only want the "error" messages, does anyone know how I can exclude these messages?
I have tried doing a grep -v but it doesn't work:
45 5 * * * /home/username/barc/backupsql.sh 2>&1 | grep -v 'Warning: Using a password on the command line interface can be insecure.'
Thanks in advance for anyone trying to help me.
Crontab lets you execute 1 scheduled command/script at a time.
Piping the output of your script to Grep command won't work. Furthermore, crontab by default redirects output to dev/null, therefore you won't see the output unless you save it to a file.
I suggest something like this:
Edit your script to redirect it's output to a file with your grep command. For example by adding
DATE=$(date +"%m_%d_%Y")
some command | grep -v Warning >> /tmp/$DATE.log # Here
Edit your Cron job to execute the script every day like you did, removing everything after the pipe:
45 5 * * * /home/username/barc/backupsql.sh
In order to monitor the output you could use tail command as follows:
tail -f /tmp/$DATE.log
I have a cron every two minutes (*/2 * * * *) firing the following command...
wget "http://www.example.com/wp-cron.php?import_key=my_key_stringimport_id=16&action=trigger"
Trouble is, it is emailing me every two minutes, and also creating copious tiny files on the server, one each time.
I have tried several things. I know there is plenty of info out there about suppressing email feedback from cron.
cPanel's Cron page, where my crons are set, makes clear: "If you do not want an email to be sent for an individual cron job, you can redirect the command’s output to /dev/null. For example: mycommand >/dev/null 2>&1"
But when I did it like this...
wget -O "http://www.example.com/wp-cron.php?import_key=my_key_stringimport_id=16&action=trigger" >/dev/null 2>&1
... the cron stopped functioning.
(I believed an -O was necessarily to direct the output).
What is the proper way to formulate this?
To suppress mails from cron you can add before your line in cron MAILTO
MAILTO=""
*/2 * * * * command
This seems to do the trick...
wget --quiet -O "http://www.example.com/wp-cron.php?import_key=my_key_stringimport_id=16&action=trigger"
Ie. Add --quiet
Answer found elsewhere on Stackoverflow.
Bit confused how --quiet and -O co-exist.
I'm trying to get the cron job to send me an email directly after the script execution and get the last logs into the body of the email
21 14 * * * /opt/anaconda/bin/python /Path/to/Script/script.py >> /Path/to/logfile/log.txt 2>&1 | mail -s "cronjob OK" "first#mail.x,second#mail.x"
How could I do that? what should I add?
Thanks in advance.
Your script is not producing any output because you just redirected it to a file.
cron will send any output by email to the owner of the cron job anyway. You can specify a different address with MAILTO in some cron implementations.
MAILTO=first#mail.x,second#mail.x
21 14 * * * /opt/anaconda/bin/python /Path/to/Script/script.py 2>&1 | tee -a /Path/to/logfile/log.txt
The tee command saves a copy of standard input to the file (-a says to append instead of overwrite) and to standard output.
If you need more control over the generated message (e.g. to use a different Subject: header if Cron's is not acceptable) maybe keep the tee but put back the pipe to mail.
I am using a crontab to run my scripts periodically, Here is the code that I have added in crontab -e
* * * * * /usr/bin/python3 /home/mark/WORKSPACE/ep_prac/scripts/main.py > $HOME/test-cron.log 2>&1; mail -s "CronJob is run successfully" abc#gmail.com, xyz#gmail.com < /home/mark/test-cron.log
I want to mail the path of "test-cron.log" file. I am running crunch of time, searched a lot but couldnt file a relevant solution
Instead of sending the path, I found a way to attach the log file itself and mail it to user.
mail -A "$HOME/test-cron.log -s "Cron Job run success" xyz#gmail.com"
I am trying crontab but crontab for email is not working.However, I want that crontab runs a shell script and that shell script runs a python script.
One crontab I tried is
* * * * * echo " This is current date and time $(date)"
but this is not getting printed on screen. I don't understand what is wrong I am doing.
for sending the email form this command.
echo " This is a message " | mailx -s "Subject" mymail#email.com
you need to make sure port 25 is open on your system.
you can use the mailq command for check email in the queue on your system.
you can check local port is open or not from
telnet localhost 25
nc localhost 25