LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   email attachment issues (https://www.linuxquestions.org/questions/linux-newbie-8/email-attachment-issues-4175545735/)

s_linux 06-18-2015 08:55 AM

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.

MensaWater 06-18-2015 09:12 AM

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

Habitual 06-18-2015 09:13 AM

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.

s_linux 06-18-2015 10:20 AM

Thanks, i understood. I will update the script. Thanks again.


All times are GMT -5. The time now is 10:02 PM.