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
Frog_1337Asked:
Who is Participating?
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
You change to grep for the percentage without the numbers, add an if to determine whether or not to send the mail message.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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
0
 
omarfaridCommented:
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
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
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?
0
 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
can you share the script to see if there is anything wrong in it?
0
 
Frog_1337Author Commented:
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
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
If you run the script from cron, is the output file created?
0
 
Frog_1337Author Commented:
I will remove the deletion of the output file and test right now
0
 
omarfaridCommented:
you are not using full path names here!

Please set PATH (and export it) at lease so it knows where to find commands
0
 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
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
0
 
Frog_1337Author Commented:
Okay when I do an echo $PATH it gives like 20 lines o I need to use all those or the first?
0
 
omarfaridCommented:
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
0
 
Frog_1337Author Commented:
(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)==>
0
 
omarfaridCommented:
in this case, please add below line to script:

PATH=/usr/bin ; export PATH

This should be before executing commands
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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 #
0
 
Frog_1337Author Commented:
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
0
 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
what is your platform & what is the user's default shell?
0
 
omarfaridCommented:
can you share how crontab job is run / configured?

also, check crontab logs for errors? sometimes errors / logs are mailed to user
0
 
omarfaridCommented:
try to redirect stdout and stderr of the crontab script to file and check for errors
0
 
Frog_1337Author Commented:
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
0
 
arnoldCommented:
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.
0
 
omarfaridCommented:
is the script executable? what do you get if you run:

ls -l /sysadm/scripts/Filemon.sh
0
 
omarfaridCommented:
did you get any error in crontab logs or user email?
0
 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
how  do you edit your crontab jobs?

Do you have any other crontab jobs running ok?

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

sh -x /sysadm/scripts/Filemon.sh

what do you get?
0
 
Frog_1337Author Commented:
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)==>
0
 
omarfaridCommented:
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
0
 
omarfaridCommented:
correction:

mailx -s 'Filesystem Report' myemailaddress < /tmp/filesysmon.txt
0
 
Abhimanyu SuriSr Database EngineerCommented:
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.
0
 
Frog_1337Author Commented:
changed #!/bin/sh to #!/usr/bin/sh

tried both alternatives given both emails were empty
0
 
omarfaridCommented:
You did not reply to some of my questions!
0
 
Frog_1337Author Commented:
Wins04 is the server name not sure what other question I missed
0
 
omarfaridConnect With a Mentor Commented:
I asked if you have other jobs running fine, what user is running the crontab job?

just to confirm, if you run script manually you get correct output sent by email, right?
0
 
Frog_1337Connect With a Mentor Author Commented:
All other cron jobs run fine. Root is running the cron job and if I run the script manually excluding the #!/usr/bin/sh or any variation there of the script works as expected. Once Cron runs it with sh it does not give the output into the email.
0
 
omarfaridConnect With a Mentor Commented:
what is the shell used by root?
0
 
omarfaridConnect With a Mentor Commented:
I can think of changing the crontab to:

30 12 * * * /usr/bin/sh /sysadm/scripts/Filemon.sh >> /sysadm/scripts/filemonerr.txt 2>&1
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Was there anything in the file when you tried my last suggestion?
0
 
Abhimanyu SuriConnect With a Mentor Sr Database EngineerCommented:
Can you please provide following details

crontab -l ---> only for the mentioned script
cat "<<script name>>"
which sh
which ksh
whoami
env
echo $PATH
ps -ef|grep -i sh
0
 
Frog_1337Author Commented:
The issue has been resolved. Running under shell the df was not giving precentages. adding a changing to df -P the egrep works flawlessly
0
 
omarfaridConnect With a Mentor Commented:
but how the script was working fine from command line?
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Does your normal shell have an alias for df?
0
 
Frog_1337Author Commented:
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
0
 
omarfaridCommented:
Welcome
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
You're welcome ;)
0
 
Frog_1337Author Commented:
One last question if wanted egrep "([89][0-9]|100)% to look for 85% or higher instead of 80% what change would I make?
0
 
arnoldConnect With a Mentor Commented:
or you have to break the pattern furtherThe first deals with 85-89, 90-99,100
egrep "([8][5-9]|[9][0-9]|100)%

or you can pull/extract the value and adjust your comparison.......as needed.
0
 
Abhimanyu SuriSr Database EngineerCommented:
This should help, provided df gives you the desired output in % format , NO ALIASES ;)

df|egrep "(8[5-9]|9[0-9]|100)%"
0
 
Frog_1337Author Commented:
Awesome sorry for the delay dealing with health issues.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
No problem at all, hope you're better now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.