LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Hi i would like to know how to use cron jobs to create three login messages. (http://www.linuxquestions.org/questions/linux-newbie-8/hi-i-would-like-to-know-how-to-use-cron-jobs-to-create-three-login-messages-913825/)

bigron1953 11-16-2011 06:54 AM

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.

unSpawn 11-16-2011 07:05 AM

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?

bigron1953 11-16-2011 08:05 AM

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.

wsteward 11-16-2011 09:34 AM

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

unSpawn 11-16-2011 11:27 AM

Quote:

Originally Posted by bigron1953 (Post 4525531)
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 (Post 4525531)
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.

suicidaleggroll 11-16-2011 11:34 AM

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.

bigron1953 11-17-2011 10:23 PM

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


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