tags:

views:

600

answers:

5

I'm using a basic shell script to log the results of top, netstat, ps and free every minute.

This is the script:

/scripts/logtop:

TERM=vt100
export TERM

time=$(date)
min=${time:14:2}

top -b -n 1 > /var/log/systemCheckLogs/$min
netstat -an >> /var/log/systemCheckLogs/$min
ps aux >> /var/log/systemCheckLogs/$min
free >> /var/log/systemCheckLogs/$min

echo "Message Content: $min" | mail -s "Ran System Check script" [email protected]

exit 0

When I run this script directly it works fine. It creates the files and puts them in /var/log/systemCheckLogs/ and then sends me an email.

I can't, however, get it to work when trying to get cron to do it every minute.

I tried putting it in /var/spool/cron/root like so: * * * * * /scripts/logtop > /dev/null 2>&1 and it never executes

I also tried putting it in /var/spool/cron/myservername and also like so: * * * * * /scripts/logtop > /dev/null 2>&1 it'll run every minute, but nothing gets created in systemCheckLogs.

Is there a reason it works when I run it but not when cron runs it?

Also, here's what the permissions look like: -rwxrwxrwx 1 root root 326 Jul 21 01:53 logtop drwxr-xr-x 2 root root 4096 Jul 21 01:51 systemCheckLogs

+1  A: 

Normally crontabs are kept in "/var/spool/cron/crontabs/". Also, normally, you update it with the crontab command as this HUPs crond after you're done and it'll make sure the file gets in the correct place.

Are you using the crontab command to create the cron entry? crontab to import a file directly. crontab -e to edit the current crontab with $EDITOR.

James
A: 

When running from /var/spool/cron/root, it may be failing because cron is not configured to run for root. On linux, root cron jobs are typically run from /etc/crontab rather than from /var/spool/cron.

When running from /var/spool/cron/myservername, you probably have a permissions problem. Don't redirect the error to /dev/null -- capture them and examine.

William Pursell
+1  A: 

All jobs run by cron need the interpreter listed at the top, so cron knows how to run them. I can't tell if you just omitted that line or if it is not in your script.

For example,

#!/bin/bash
echo "Test cron jon"
James
A: 

Something else to be aware of, cron doesn't initialize the full run environment, which can sometimes mean you can run it just fine from a fully logged-in shell, but it doesn't behave the same from cron.

In the case of above, you don't have a "#!/bin/shell" up top in your script. If root is configured to use something like a regular bourne shell or cshell, the syntax you use to populate your variables will not work. This would explain why it would run, but not populate your files. So if you need it to be ksh, "#!/bin/ksh". It's generally best not to trust the environment to keep these things sane. If you need your profile run the do a ". ~/.profile" up front as well. Or a quick and dirty way to get your relatively full env is to do it from su as such "* * * * * su - root -c "/path/to/script" > /dev/null 2>&1

Just some things I've picked up over the years. You're definitely expecting a ksh based on your syntax, so you might want to be sure it's using it.

James
A: 

Thanks for the tips... used a little bit of each answer to get to the bottom of this.

I did have the interpreter at the top (wasn't shown here), but may have been wrong. Am using #!/bin/bash now and that works.

Also had to tinker with the permissions of the directory the log files are being dumped in to get things working.

Susan