Top output to textfile not working in cronjob

Hello,
If i run the following script from the cmdline it works just fine however if i run it via root cronjob it doesn't write to the file at all. It is chmod 777 now so shouldnt be any right issues.

Shellscript looks like this:

/usr/bin/top -b -n 1 -u nagios >> /var/log/test.log

Cronjob like this:

38 * * * * sh /shellScripts/test.sh >> /var/log/dummy.txt

Any idea?
Simply i want to log top output to a textfile when cronjob runs.

Any output would go to /var/log/test.log, not /var/log/dummy.txt. Is that where you are checking?

Also, in this case errors, would to the job owner's email (usaually).

Ye i am checking in the test.log file.
It writes there just fine if i do it from cmdline, not sure why cronjob isn't picking it up. Is there anyway to see what the cronjob says about it?

And the location of the script is definitely /shellScripts/test.sh? The directory is at least r-x for the user running the cronjob?

Can you check /var/log/cron to check the job actually ran?

I ran a text doing (albeit in RedHat) exactly what you said you are doing and have no problems.

1 Like

does your script have PATH variable set or at least the commands have their full paths (i.e., /bin/hostname instead of hostname , /usr/bin/rm instead of just rm , etc.)? if yes, look at something else in your current environment that will not be there should cron run your script and then account for it inside your script ...

Yep the directory is read and execute for users.
If i check the syslog i saw the following:

Aug  8 18:03:01 ubuntuServer CRON[21762]: (CRON) info (No MTA installed, discarding output)
Aug  8 18:05:20 ubuntuServer crontab[21804]: (root) BEGIN EDIT (root)
Aug  8 18:05:40 ubuntuServer crontab[21804]: (root) REPLACE (root)
Aug  8 18:05:40 ubuntuServer crontab[21804]: (root) END EDIT (root)
Aug  8 18:05:42 ubuntuServer crontab[21816]: (root) BEGIN EDIT (root)
Aug  8 18:06:01 ubuntuServer cron[1335]: (root) RELOAD (crontabs/root)
Aug  8 18:06:01 ubuntuServer crontab[21816]: (root) REPLACE (root)
Aug  8 18:06:01 ubuntuServer crontab[21816]: (root) END EDIT (root)
Aug  8 18:07:01 ubuntuServer cron[1335]: (root) RELOAD (crontabs/root)
Aug  8 18:07:01 ubuntuServer CRON[21841]: (root) CMD (sh /shellScripts/test.sh >/dev/null 2>&1)

Got that "No MTA Installed" so i added the "/dev/null 2>&1" just to see the difference.

---------- Post updated at 11:19 AM ---------- Previous update was at 11:13 AM ----------

I've not set any special path variables.
The entire scriptfile looks like this:

#!/bin/sh
clear
/usr/bin/top -b -n 1 -u nagios >> /var/log/test.log

the MTA (mail transfer agent) issue does not seem to be related to your script as it shows up in the logs more than a few minutes before your script runs and the script/program creating the log entries is doing some editing -- most likely a sql maintenance script -- while your top script does not ... besides, the script you posted does not send email so there would be no need for MTAs ...

change 2> /dev/null in your cronjob listing to 2> /shellScripts/test.err.log and see if there are any errors logged in the error log ...

1 Like

Hi again,
I've no idea what actually made it work but i tried a ton of stuff including both of your suggestions so something obviously did the trick. I'm grateful for your assistance, and if i find out the specific reason why it now works i'll post it here for somebody else in the future.