LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-18-2015, 02:40 AM   #1
GioBonvi
Member
 
Registered: Feb 2015
Location: Trento, Italy
Distribution: Ubuntu 15.04, Linux Mint 17.2
Posts: 36

Rep: Reputation: Disabled
Unhappy Crontab - ssmtp script doesn't send email


Hello everyone!
In the last few months I've been working on a little server for home purposes (VPN, little site-project to be used in LAN, network boot PXE server and some other things).
My main objective is to learn something new, rather than build something really useful.

Yesterday I installed ssmtp on my server and configured it to use my secondary gmail address (let's say second@gmail.com)
Everything went really fine and I was able to send an email by doing:
Code:
ssmtp first@gmail.com
First line
Second line
^Q
or
Code:
echo "First line
second line" | ssmtp first@gmail.com
The problem raised when I tried to put such a job in crontab.
I did
Code:
crontab -e
and added this line to crontab:
Code:
*/10 * * * * /home/user/test.sh
Where test.sh was an executable file:
Code:
#!/bin/bash
echo "FIRST TRY First line
second line" | ssmtp first@gmail.com
echo "SECOND TRY First line" | ssmtp first@gmail.com
But nothing happened... I tested crontab by replacing "/home/user/test.sh" with "echo 1 > /home/user/list" and it worked. Then I tried to put "sh /home/user/test.sh" but there was no difference.
Finally I tried executing manually the script and IT SENT THE MAIL!

I don't know what to do...
 
Old 10-18-2015, 05:48 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I don't know ssmtp, but perhaps it expects certain environment variables which are not set in cron's environment. Find out if that is the case and set those variables in the script.

Futhermore, if you don't redirect stout and stderr to a file, cron will send you ssmtp's messages via email. So check your email, and modify the script to redirect IO to a file.
 
Old 10-18-2015, 11:07 AM   #3
GioBonvi
Member
 
Registered: Feb 2015
Location: Trento, Italy
Distribution: Ubuntu 15.04, Linux Mint 17.2
Posts: 36

Original Poster
Rep: Reputation: Disabled
Problem SOLVED

Quote:
Originally Posted by berndbausch View Post
I don't know ssmtp, but perhaps it expects certain environment variables which are not set in cron's environment. Find out if that is the case and set those variables in the script.

Futhermore, if you don't redirect stout and stderr to a file, cron will send you ssmtp's messages via email. So check your email, and modify the script to redirect IO to a file.
Thank you, berndbausch!

You were right! I googled for "ssmtp and cron" (which I hadn't thought of) and immediately found this:
http://askubuntu.com/questions/382049/crontab-and-ssmtp

It seems that in the cron environment you need to specify the full ssmtp path (/usr/sbin/ssmtp) instead of only ssmtp
Not sure if this is a bug or if there is a definitive solution instead of this "workaround", but I'm definetely OK with this solution.

Thank you again. Problem solved!
 
Old 10-18-2015, 09:18 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
By default, cron's env is deliberately minimal (security etc), so this is a common gotcha for anything run inside cron.
Best to either provide full paths to cmds and files or write a script and call that from cron (more robust & private) and have the script set whatever it needs for that job.
 
  


Reply

Tags
crontab, ssmtp



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script Automatic send email using crontab samkorok Linux - Newbie 1 04-29-2015 12:55 PM
send mail to all email-id's in a file using ssmtp aavika Linux - Newbie 3 12-10-2014 06:23 AM
[SOLVED] Send email at a particular time without using crontab sysmicuser Linux - Newbie 5 03-13-2012 07:20 AM
help- how to reduce Size of Email attachment send via Crontab pnandak Linux - Newbie 18 02-29-2012 06:12 PM
Script run by crontab won't send email Braynid Linux - Newbie 1 12-21-2007 12:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration