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 11-16-2011, 07:54 AM   #1
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
Smile Hi i would like to know how to use cron jobs to create three login messages.


I am running centOS 5.7 (bash shell), What i am trying to do is to set it up so that each week on tuesday if users log in between 5PM and 6PM,a message would be displayed. that's about it.

Thank you much.
 
Old 11-16-2011, 08:05 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
If I assert it's command line logins you're targeting, then when you look at 'man crontab; man 5 crontab; man issue; man motd' do you think you can come up with (pseudo) code for this or otherwise show you've got any idea for trying?
 
Old 11-16-2011, 09:05 AM   #3
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi, from crontab -e the code that i used is:

00 17,18 * * 2 echo "Welcome and please remember we are working on math today." > /etc/motd
00 18,19 * * 2 echo "Welcome i hope you are ready for some math today." > /etc/motd
00 19,20 * * 2 echo "Welcome i know its geting late, but never to late for math right?" > /etc/motd

and what i'm trying to do here is echo the message and redirect and overwrite the /etc/motd file.
But when i try it out it does not work because i am doing someting wrong and it will not redirect.
Please if you need more info please let me know thanks much.
 
Old 11-16-2011, 10:34 AM   #4
wsteward
LQ Newbie
 
Registered: Feb 2007
Posts: 9

Rep: Reputation: 2
Another way to handle this is to use a script file to post the message in your motd file. It could be 3 simple executable scripts:

math

#!/bin/sh
#**************************************************************************#
echo "Welcome and please remember we are working on math today."
echo "Welcome and please remember we are working on math today." > /etc/motd
#**************************************************************************#
exit

math1

#!/bin/sh
#**************************************************************************#
echo "Welcome i hope you are ready for some math today."
echo "Welcome i hope you are ready for some math today." > /etc/motd
#**************************************************************************#
exit

math2

#!/bin/sh
#**************************************************************************#
echo "Welcome i know its geting late, but never to late for math right?"
echo "Welcome i know its geting late, but never to late for math right?" > /etc/motd
#**************************************************************************#
exit

Schedule these 3 script as you previously have.

00 17,18 * * 2 /bin/bash /path_to_script/math
00 18,19 * * 2 /bin/bash /path_to_script/math1
00 19,20 * * 2 /bin/bash /path_to_script/math2
 
Old 11-16-2011, 12:27 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Quote:
Originally Posted by bigron1953 View Post
from crontab -e the code that i used is:

00 17,18 * * 2
Are you sure 'man 5 crontab' says Tuesday is 2?...


Quote:
Originally Posted by bigron1953 View Post
But when i try it out it does not work
Unless one mucks with the MAILTO variable a cronjob will send stderr to mail the crontabs owner so check your mailbox. If you aren't root (or use /etc/crontab) then ownership and access permissions deny you overwriting /etc/motd. BTW you don't need three scripts or Christmas lights comments:
Code:
#!/bin/bash
CRONTARGET="/etc/motd"
[ $(id -u) -eq 0 ] || { logger -t cronjob "I am not root and I will exit now."; exit 127; }
_help() { progn=${0//*\//}; progn=${progn//.*/}; echo "${progn}: [ message number: [ -0, -1 or -2 ] or [ -r(eset) MOTD | -h(elp) ]"; exit 1; }
[ $# -eq 1 ] || _help
while getopts 012rh OPT; do case "${OPT}" in
        0) CRONMESG="Welcome and please remember you are working on Math today." ;;
        1) CRONMESG="Welcome, I hope you are ready for some Math today." ;;
        2) CRONMESG="Welcome. I know it is getting late but never too late for Math, right?" ;;
        r) CRONMESG="";;
        h) _help;; esac; done
echo "${CRONMESG}" > "${CRONTARGET}" 2>/dev/null
exit 0
Save the script as /usr/local/bin/mathmsg.sh then call from /etc/crontab as "00 17,18 * * 1 /usr/local/bin/mathmsg.sh -0" (select message 0, 1 or 2 at the appropriate time) and note that '/usr/local/bin/mathmsg.sh -r' will reset (empty) /etc/motd.
 
Old 11-16-2011, 12:34 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,144

Rep: Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108
Part of the problem could be your cron scheduling. You're printing message 1 at both 17 and 18 UT, message 2 at both 18 and 19 UT, and message 3 at both 19 and 20 UT. This means that message 1 AND message 2 will both be trying to write to motd at 18 UT, and message 2 AND message 3 will both be trying to write to motd at 19 UT. This could lead to some interesting behavior.
 
1 members found this post helpful.
Old 11-17-2011, 11:23 PM   #7
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you all.

The last post:

Part of the problem could be your cron scheduling. You're printing message 1 at both 17 and 18 UT, message 2 at both 18 and 19 UT, and message 3 at both 19 and 20 UT. This means that message 1 AND message 2 will both be trying to write to motd at 18 UT, and message 2 AND message 3 will both be trying to write to motd at 19 UT. This could lead to some interesting behavior.


is where i was going wrong so i fixed it. And yes Tuesday is 2

Thank you all very much
 
  


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
cron hourly, daily, cron.d jobs don't execute eggsmartha Linux - General 3 09-17-2007 07:37 PM
Cron Jobs christo512 Linux - Software 1 02-09-2006 10:19 AM
Cron Jobs XaViaR Linux - General 1 06-23-2005 07:57 PM
cron jobs working2hard Linux - Software 0 07-28-2004 06:31 PM
cron jobs Zyanid Linux - Software 0 08-27-2002 06:10 AM


All times are GMT -5. The time now is 08:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration