Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scritp runs fine, but not from CRON 1

Status
Not open for further replies.
May 31, 2006
237
US
So I have this script and this is what it does:

Code:
#!/bin/sh
#
# Active Sessions - displays number of active sessions and puts
# a new entry into a log file.
#

# Adds a line (for readability) and datestamp into log
echo '' >> /home/luminis/scripts/sessions.log
date >> /home/luminis/scripts/sessions.log

# Checks active sessions and only reports back on the numbers
# (without grep command, it will also log all active sessions by
# username)
cptool list sessions | grep ctive >> /home/luminis/scripts/sessions.log

That last line is all the information I need. Now, when I run it this is the kind of information I get in the sessions.log file:

Wed Sep 2 14:04:06 EDT 2009
Active sessions on {webportal-2}: 97
Active sessions on {resourceserver}: 0
Active sessions on {webportal-1}: 96
Total number of active sessions: 193

Wouldn't it be neat if I could set that in a CRON job to run every 15 minutes and log the number of active sessions so I could have a history? Yes! However, when I put it into crontab:

*/15 * * * * /home/luminis/scripts/active > /dev/null 2>&1

Yes it does in fact run every 15 minutes and it does everything... except the last line. It looks like this in the log:


Wed Sep 2 14:15:01 EDT 2009

And that's it. So I checked the sudoers file and added the command /opt/luminis/cptool for the currently logged in user, but it still doesn't do anything after adding the entry. (Yes I used visudo to edit sudoers)

Is there something I need to do to get the edited sudoers file to take effect? Or is there something else I'm missing?

thanks in advance!
 
Try using a full pathname for your cptools command.
(i.e)
/usr/sbin/cptools list sessions (... etc)
Your actual pathname will probably be different, this is just an example.

Also, you can direct the output of the command to a file instead of /dev/null to see if there is an error being generated.

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Awesome, thanks for the suggestions. I'll give it a try tomorrow and let you know how it works (-:
 
It didn't work when I put the full path in the script to the cptool executive. After I enabled the error log in cron, it occurred to me why it wasn't running. Invoking the script in cron makes it run in a separate shell, which means that if there's an environment set up under this user it's not set up in the separate shell. There's a script that runs to set up the environment like this:

. /opt/luminis/.cprc

This sets up the correct environment variables so that cptool will run correctly. Once I added that line to the script, the next time it ran in cron it ran just like it was supposed to.

Thu Sep 3 09:00:01 EDT 2009
Active sessions on {webportal-2}: 52
Active sessions on {resourceserver}: 0
Active sessions on {webportal-1}: 52
Total number of active sessions: 104

Thanks again for your suggestions and getting me to think about what I was trying to accomplish (-:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top