how to run a script using cron job and send the output as attachment via e-mail using unix

how to run a script using cron job and send the output as attachment via e-mail using unix. please help me.

how my cron job entry should be?

As of now my cron job entry is to run the script at specific time,

15 03 * * * /path/sample.sh | mail -s "Logs" email_id

In the above entry, what i have to change inorder to send the output file also as the attachment.

output file name is output.gz

Thanks in Advance!

to attach a file, you off course need the file.
Either you first create a log file with "/path/sample.sh".
This could be done with a wrapper.

wrapper.sh:

#!/bin/......
/path/sample.sh >> /path/log_file
(cat /path/log_file ; uuencode /path/log_file log_file) | mailx -m -s "Logs" email_id

Now schedule this wrapper.

Thanks for the quick reply!

i just integrated the pattern in my script,
(cat /path/log_file ; uuencode /path/log_file log_file) | mailx -m -s "Logs" email_id

but the following error thrown,

mailx: invalid option -- m
Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...
            [-- sendmail-options ...]
       mail [-iInNv] -f [name]
       mail [-iInNv] [-u user]
/path/sample.sh: line 47: uuencode: command not found

Am using Bash shell script!

1) mailx doesn't support -m option. (optional, should work without too)
2) Perhaps uuencode is not installed on your system. (require)

You might need to get it done first. or look for any other tool/source.

Am using linux box of a server. So i cant install it the server.

Is there any alternative way to send a attachment file via email?

---------- Post updated 06-09-11 at 01:00 AM ---------- Previous update was 06-08-11 at 07:24 AM ----------

if there is no alternative way, can u pls let me know how to install uuencode in the linux box using putty?

try mutt

mutt -s "Log file" -a log_file.txt all@all.org < mail_contents 

Error thrown:
mutt command not found

how i have to install mutt or uuencode in my linux box using putty?

uuencode is in sharutils:

(example, for Red Hat, ...)

# yum whatprovides */uuencode 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
...
...
...
sharutils-4.6.1-2.x86_64 : The GNU shar utilities for packaging and unpackaging shell archives.
Repo        : base
Matched from:
Filename    : /usr/bin/uuencode
# yum install sharutils
2 Likes

Thanks scottn :slight_smile:

It works now!