LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-18-2015, 09:55 AM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
email attachment issues


Hello,
I have a small bash script that sends email with *.csv attached file. it works when i manually run it. I get the email with attachment. when i set up a cronjob, script runs at specified time and i received email with attachment but file is blank and i suspect it is not doing what script supposed to do as i dont see file created with time stamp
Quote:
#!/bin/bash
set -x
echo "Attached the user report" | mail -s "User Report" -a report.csv user1@xxx.net user2@xxx.net user3@xxx.net

DateTimeStamp=$(date '+%d_%m_%y_%H_%M_%S')
mv report.csv report.csv.$DateTimeStamp
touch eport.csv
any idea on why it is sending blank file though the actual file has content and not renaming the file with timestamp. Thanks.
 
Old 06-18-2015, 10:12 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,075
Blog Entries: 14

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
My guess is your issue here is actually it doesn't know what directory the original file is in because you haven't told it to go there. If you're doing this in your home directory ($HOME) that may NOT be where cron is doing it. You can specify the directory in your commands e.g.
mv /<directory>/report.csv /<directory>/report.csv.$DateTimeStamp

However you'd have to do that for every command so it would be simpler to do change directory at the beginning of the script.
cd /<directory>

The environment you get when you run a shell script at command line usually has far more in it than the minimal one cron uses.

Your script uses commands: mail, mv, date and touch.

You need to insure you have a PATH in cron that includes the directory for any commands that you will use. You can determine the directories by typing "which <command>" for each at command line then you can add PATH to your script e.g.
PATH=$PATH:/bin:/usr/bin:/usr/local/bin
Generally most of those commands are in /bin and that usually IS in the minimal path cron uses so this is why I'm guessing issue is directory instead but I wanted to note it.

That would tell it to use whatever PATH cron uses by default then also add directories /bin, /usr/bin and /usr/local/bin to be searched.

You should also run "alias" at command line to see if any of the above commands are aliased to do additional steps e.g.
Often you'll see the ls command is aliased:
alias ls='ls --color=tty'

If you saw an alias for mail, mv, date or touch you might need to specify the full flags in your script as it wouldn't have the aliases.

PATH is the most common missing variable but there could be others in your environment at command line that you need to add to your script. You can see your full environment by typing "env" at command line.


By the way your final line appears to be missing the "r" in report:
touch eport.csv
 
Old 06-18-2015, 10:13 AM   #3
Habitual
LQ 5k Club
 
Registered: Jan 2011
Location: Nowhere near you, thank God.
Distribution: High Sierra
Posts: 8,598
Blog Entries: 15

Rep: Reputation: Disabled
bash scripts via cron have a limited bash environment.
Try /path/to/mail and /path/to/report.csv and
Code:
#DateTimeStamp=$(date '+%d_%m_%y_%H_%M_%S')
mv /path/to/report.csv /path/to/report.csv$(date '+%d_%m_%y_%H_%M_%S')
in the script.
Code:
DateTimeStamp=$(date '+%d_%m_%y_%H_%M_%S')
isn't needed IMO.
 
Old 06-18-2015, 11:20 AM   #4
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks, i understood. I will update the script. Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Email attachment handling rmockler Linux - Newbie 3 01-26-2014 06:54 PM
convert email files with attachment to save as attachment file (metamail?) ted_chou12 Linux - Software 2 04-08-2011 09:01 PM
Cron email with attachment ahingert Linux - Newbie 11 09-22-2010 10:52 AM
email attachment lhrt Linux - Networking 3 05-31-2006 06:13 AM
cannot receive email with attachment charles168 Linux - Software 2 09-06-2003 10:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration