Link to home
Start Free TrialLog in
Avatar of Frog_1337
Frog_1337

asked on

UNiX Script filesystem space usage Part2

I had asked for assistance creating a script to display filesystem usage over a certain percentage. The script works when ran manually. However it when run from Cron the email is blank. Cron states it is running it as /bin/sh so I have attempted #!/bin/sh #!/bin/bash #!/bin/ksh and every variation I could think of such as #!/use/bin/sh  If it has the interpreter at the beginning of the script the emails will always be empty. I am trying to get this as a daily generated email. I even attempted creating a second script to run from cron that simply executed this script and the emails are empty. Assistance would be greatly appreciated.



output=$(df | egrep "([89][0-9]|100)%")
echo ${output} >> filesysmon.txt
mailx -s "Servername Filesystem Report" "Email@email.com < filesys
mon.txt
rm -fr filesysmon.txt
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

You need to add full path to programs and files that you use, so /tmp/filesysmon.txt instead of filesysmon.txt etc.

https://www.experts-exchange.com/articles/17919/How-to-setup-a-crontab-job-in-Linux-Unix.html
for crontab jobs / scripts:

1- set required env. variables
2- use full path names to files / directories / command3
3- take care of redirection of stdin , stdout, and stderr
Avatar of Frog_1337
Frog_1337

ASKER

Yeah I have set scripts for cron before. This is the first time I have encountered the issue. Even if I add the interperter and run the script manually the email is empty. I updated the script with full paths to files added #!/bin/sh ran it manually the email is empty
I think there is something wrong in the script:

output=$(df | egrep "([89][0-9]|100)%")
echo ${output} >> filesysmon.txt
mailx -s "Servername Filesystem Report" "Email@email.com < filesys
mon.txt
rm -fr filesysmon.txt

is the break in file name there? or it is because of wrapping?
It is because of wrapping. The script runs perfect when executed normally. As soon as added to cron, cron gives the message will execute as sh so it does not work. if I had #!/bin/ANYTHING the script stops working the email is blank
can you share the script to see if there is anything wrong in it?
This is the entire script

output=$(df | egrep "([89][0-9]|100)%")
echo ${output} >>/tmp/filesysmon.txt
mailx -s "Filesystem Report" "myemailaddress" < /tmp/filesysmon.txt
rm -fr /tmp/filesysmon.txt
If you run the script from cron, is the output file created?
I will remove the deletion of the output file and test right now
you are not using full path names here!

Please set PATH (and export it) at lease so it knows where to find commands
The file is being created but it has no data when run from cron.


And when you say set the path can you elaborate Im not quiet understanding
Please note that shell will be looking for commands in directories set in PATH env. variable.

you can do the following, from sell prompt, run

echo $PATH

example ourput will be

/bin:/usr/bin

take the output and add to your script:

PATH=/bin:/usr/bin ; export PATH
Okay when I do an echo $PATH it gives like 20 lines o I need to use all those or the first?
usually /bin:/usr/bin should be enough. You can run below from command line and see the minimum required:

which egrep
which df
which mailx
which rm

Share the out put of above commands and I will show you how to add to script
(359)==> which egrep
/usr/bin/egrep
wins04:root:[/sysadm/scripts]
(360)==> which df
/usr/bin/df
wins04:root:[/sysadm/scripts]
(361)==> which mailx
/usr/bin/mailx
wins04:root:[/sysadm/scripts]
(362)==> which rm
/usr/bin/rm
wins04:root:[/sysadm/scripts]
(363)==>
in this case, please add below line to script:

PATH=/usr/bin ; export PATH

This should be before executing commands
Just try this:
/usr/bin/df | /usr/bin/egrep "([89][0-9]|100)%" > /tmp/filesysmon.txt
/usr/bin/mailx -s "Filesystem Report" "myemailaddress" < /tmp/filesysmon.txt
#rm /tmp/filesysmon.txt

Open in new window

If you get a mail that has the contents you expect, remove the #
Exporting the path the email is still empty when run from CRON. The script does not like the use /bin/sh to run it. I will try Gerwin's  method and see if it is any differnet
When trying Gerwin's script manually I get

(373)==> ./testfile.sh
Null message body; hope that's ok
wins04:root:[/sysadm/scripts]

and blank email

From CRON blank email

Manually I have tried running using #!/bin/sh #!/bin/bash #!/bin/ksh

with those it will always be blank
what is your platform & what is the user's default shell?
can you share how crontab job is run / configured?

also, check crontab logs for errors? sometimes errors / logs are mailed to user
try to redirect stdout and stderr of the crontab script to file and check for errors
30 12 * * * /sysadm/scripts/Filemon.sh >/sysadm/scripts/filemonerr.txt 2>&1


warning: commands will be executed using /usr/bin/sh
wins04:root:[/sysadm/scripts]
nothing is ever in the filemonerr.txt
Mails, mail, mutt, are Emil clients, I would recommend you familiarize yourself with using sendmail as th mechanism through which email is generated/sent.

This injects the message directly into the processing queue.........

You can define a function to which you can pass different info.
is the script executable? what do you get if you run:

ls -l /sysadm/scripts/Filemon.sh
did you get any error in crontab logs or user email?
The script is executable I have been running it manually


-rwxrwxr-x   1 root       sys            243 Aug  2 12:45 Filemon.sh

No errors present. It simply does add the text to the email.

Platform hpux 11.1
how  do you edit your crontab jobs?

Do you have any other crontab jobs running ok?

Are you using root account to run crontab?
if you run script with debug option:

sh -x /sysadm/scripts/Filemon.sh

what do you get?
418)==> sh -x Filemon.sh
+ + egrep ([89][0-9]|100)%
+ df
output=
+ echo
+ 1>> /tmp/filesysmon.txt
+ mailx -s Wins04 Filesystem Report My email address
+ 0< /tmp/filesysmon.txt
+ rm -fr /tmp/filesysmon.txt
wins04:root:[/sysadm/scripts]
(419)==>
Please compare below:

mailx -s "Filesystem Report" "myemailaddress" < /tmp/filesysmon.txt

mailx -s Wins04 Filesystem Report My email address

from where Wins04 comes?

Please change

mailx -s "Filesystem Report" "myemailaddress" < /tmp/filesysmon.txt

to

mailx -s 'Filesystem Report myemailaddress < /tmp/filesysmon.txt
correction:

mailx -s 'Filesystem Report' myemailaddress < /tmp/filesysmon.txt
Please do

which sh
/bin/sh

What ever path comes, put it in ur script , example :

#!/bin/sh
echo `df | egrep "([89][0-9]|100)%"` |mailx -s "USAGE" abhimanyu.suri@expert-exchange.com

If I replace "#!/bin/sh with #!/usr/bin/sh" , it stops working from CRON.

It exclusively depends on your system settings.
changed #!/bin/sh to #!/usr/bin/sh

tried both alternatives given both emails were empty
You did not reply to some of my questions!
Wins04 is the server name not sure what other question I missed
SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The issue has been resolved. Running under shell the df was not giving precentages. adding a changing to df -P the egrep works flawlessly
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After checking yeah there was an alias for df to bdf its on all the machines this script is going on. THank you guys so much for your help and patience I was going nuts because I knew the script was correct. Thank you again
Welcome
You're welcome ;)
One last question if wanted egrep "([89][0-9]|100)% to look for 85% or higher instead of 80% what change would I make?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This should help, provided df gives you the desired output in % format , NO ALIASES ;)

df|egrep "(8[5-9]|9[0-9]|100)%"
Awesome sorry for the delay dealing with health issues.
No problem at all, hope you're better now.