LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2006, 01:53 AM   #1
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Rep: Reputation: 15
schedule email


is there a way to write an email via bash scripting then for it to schedule it to send say once a month

the bash script say asks some qustions that are then put into an email then the script sends the email on the date that it asks via the bash script

thanks
 
Old 01-25-2006, 04:52 AM   #2
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
use mailx or mail,
in your bash script

schedule it to trigger once in every month in your cron job.

could you please post your requirement with some sample input and output?
 
Old 01-25-2006, 10:45 AM   #3
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
what i want it to do

get user input from a user via bash say

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

sed "s/mail/$mail/g" mail.sh > mail1.sh
rm -rf mail.sh
sed "s/mess/$mess/g" mail1.sh > mail2.sh
rm -rf mail1.sh
sed "s/sub/$sub/g" mail2 > mail.sh
rm -rf mail2.sh

echo "ok mail will be sent on $day of every month"

then i want it to edit another bash script with the info which i know how to do then place that bash script in cron to run on the date specified by the user in the varible $day


the other bash looks like this

#! /bin/bash

./sendmail -f admin@blah.com -t letter -m mess -u sub

thanks

Last edited by rharris72; 01-25-2006 at 11:14 AM.
 
Old 01-25-2006, 09:32 PM   #4
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
can anyone help with this please thanks ?
 
Old 01-25-2006, 11:00 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
This is what I would do:

Code:
#!/bin/bash

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

echo "0 23 $day * * cat \"$mess\" | mail -s \"$sub\" $letter" > /tmp/cron.mail
crontab /tmp/cron.mail

And here are the results:

twantrd@twantrd:~$ /tmp/test
enter subject
Hi there
enter message
How are you?
mail to
me@mydomain.com
please enter day of month to send mail
15
twantrd@twantrd:~$ crontab -l
0 23 15 * * cat "How are you?" | mail -s "Hi there" me@mydomain.com

So, this is just one script that does all the work for you and it's pretty simple once you look at it. This will set the cronjob for that user to mail out that message at 11:00pm on the 15th of every month.

-twantrd
 
Old 01-26-2006, 02:27 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
echo "0 23 $day * * cat \"$mess\" | mail -s \"$sub\" $letter" > /tmp/cron.mail
crontab /tmp/cron.mail
appending to the existing entries in cron file would be the best option.
 
Old 01-26-2006, 05:33 AM   #7
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
how would i enter that cron command if i wanted it to run a bash script

thanks for all your help

i think linux people are far more helpful than dudes that use windows
 
Old 01-26-2006, 07:15 AM   #8
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
after the saving the functionality in the bash script

edit your crontab file

crontab -e

and add the entry,
Code:
0 23 * * * <bashscriptname>
 
Old 01-26-2006, 10:18 AM   #9
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
script ./mail1.sh

#!/bin/bash
chmod 777 /home/mailshot -R

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

cd /home/mailshot

sed "s/letter/$letter/g" mail.sh > mail1.sh
sed "s/mess/$mess/g" mail1.sh > mail2.sh
rm -rf mail1.sh
sed "s/sub/$sub/g" mail2.sh > $clan.sh
rm -rf mail2.sh

echo "ok mail will be sent on $day of every month"

new="10 16 $day * * /home/mailshot/$name.sh"

crontab /home/mailshot/mail.cron

mail=`cat /home/mailshot/mail.cron`

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1
# open file "mail.cron" for writing
exec 1>mail.cron
# produce some content
echo "$mail"
echo "$new"
# close file "mail.cron"
exec 1>&-
# make stdout a copy of FD 6 (reset stdout)
exec 1>&6
# close FD6

exec 6>&-
exit 0


script mail.sh contains this

#! /bin/bash

/home/mailshot/sendmail -f admin@blah.com -t "letter" -m "mess" -u "sub"

then i have the sendmailbin file in same dir and also a file mail.cron but for some reason its not showing cron jobs in webmin for admin user once i exec script

also i dont want it to send a mail to admin evertime it exec the cron job

can anyone help

cheers

i know scripts are a bit messy but learning
 
Old 01-26-2006, 03:59 PM   #10
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
please help if you can i have added the scripts im using as to show you what im doing or trying to do

thanks

if i use this code then it only processes the last inputed crontab ??????

#!/bin/bash

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day


echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "$letter" -m "$mess" -u "$sub"" > admin.cron
echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "jerry@blah.co.uk" -m "$mess" -u "$name $sub"" > admin1.cron
echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "phil@blah.co.uk" -m "$mess" -u "$name $sub"" > admin2.cron

mv admin.cron /home/mailshot/
mv admin1.cron /home/mailshot/
mv admin2.cron /home/mailshot/

crontab /home/mailshot/admin.cron
crontab /home/mailshot/admin1.cron
crontab /home/mailshot/admin2.cron

Last edited by rharris72; 01-26-2006 at 04:02 PM.
 
Old 01-27-2006, 02:22 AM   #11
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Quote:
if i use this code then it only processes the last inputed crontab ??????
That is correct with what you have as cron will read from the last file. Put 'admin.cron', 'admin1.cron', and 'admin2.cron' into a single file and then crontab that file.

-twantrd
 
Old 01-27-2006, 05:26 AM   #12
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
ok if i put contents of 3 files into one file would i be able to specify diff times and days for the mails to be sent ?

also how would i go about echoing content to one file i know your prob thinking this guy is a nood

well the answer to that is yes yesy yes but i love learning linux tried learning windows and gave up

thanks in advance
 
Old 01-27-2006, 07:16 AM   #13
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
is there a way of putting the 3 lines of cron into 1 line and use bash newline command which i have read about but dont understand how to use it ?
 
Old 01-27-2006, 08:33 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you shouldn't really write directly to crontab files.
not really what they're designed for.

when you use 'crontab -e' it checks it and reads it.
not sure if cron would register the crontab update
if you write it like so, unless you did a crontab -e afterwards.

I would strongly discourage it.
just what I think
 
Old 01-27-2006, 02:33 PM   #15
rharris72
Member
 
Registered: Nov 2005
Posts: 32

Original Poster
Rep: Reputation: 15
proberbly long winded but i did it this way and it worked so thanks to all that helped but if there is an easier way can someone elaborate thanks....


#!/bin/bash

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day


echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "$letter" -m "$mess" -u "$sub"" > /home/mailshot/1.txt
echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "jerry@blah.co.uk" -m "$mess" -u "$name $sub"" > /home/mailshot/2.txt
echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "phil@blah.co.uk" -m "$mess" -u "$name $sub"" > /home/mailshot/3.txt

a=`cat /home/mailshot/1.txt`
b=`cat /home/mailshot/2.txt`
c=`cat /home/mailshot/3.txt`
z=`cat /home/mailshot/mail.cron`

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1
# open file "mail.cron" for writing
exec 1>/home/mailshot/mail.cron

# produce some content
echo "$z"
echo "$a"
echo "$b"
echo "$c"

# close file "mail.cron"
exec 1>&-
# make stdout a copy of FD 6 (reset stdout)
exec 1>&6
# close FD6
exec 6>&-

crontab /home/mailshot/mail.cron
rm -rf /home/mailshot/1.txt
rm -rf /home/mailshot/2.txt
rm -rf /home/mailshot/3.txt
exit 0
 
  


Reply



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
schedule problem omerlh Linux - Software 1 01-11-2006 02:15 PM
Schedule a task ! L1nuxbug Linux - Networking 4 03-02-2005 07:54 PM
Mandrake 10.1 Schedule is Up colnago Mandriva 0 03-25-2004 01:29 PM
Schedule, AGAIN!! erathomor Linux - General 0 04-11-2002 11:25 AM
HOW TO Schedule erathomor Linux - General 3 04-10-2002 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 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