Solved

UNiX Script filesystem space usage Part2

Posted on 2016-08-02
54
72 Views
Last Modified: 2016-08-12
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
0
Comment
Question by:Frog_1337
  • 22
  • 19
  • 8
  • +2
54 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41738922
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41738932
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
 

Author Comment

by:Frog_1337
ID: 41738963
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41738972
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
 

Author Comment

by:Frog_1337
ID: 41738994
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41738996
can you share the script to see if there is anything wrong in it?
0
 

Author Comment

by:Frog_1337
ID: 41738999
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41739002
If you run the script from cron, is the output file created?
0
 

Author Comment

by:Frog_1337
ID: 41739010
I will remove the deletion of the output file and test right now
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739013
you are not using full path names here!

Please set PATH (and export it) at lease so it knows where to find commands
0
 

Author Comment

by:Frog_1337
ID: 41739026
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739036
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
 

Author Comment

by:Frog_1337
ID: 41739066
Okay when I do an echo $PATH it gives like 20 lines o I need to use all those or the first?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739072
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
 

Author Comment

by:Frog_1337
ID: 41739101
(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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739261
in this case, please add below line to script:

PATH=/usr/bin ; export PATH

This should be before executing commands
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41739280
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
 

Author Comment

by:Frog_1337
ID: 41739299
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
 

Author Comment

by:Frog_1337
ID: 41739313
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739318
what is your platform & what is the user's default shell?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739326
can you share how crontab job is run / configured?

also, check crontab logs for errors? sometimes errors / logs are mailed to user
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739342
try to redirect stdout and stderr of the crontab script to file and check for errors
0
 

Author Comment

by:Frog_1337
ID: 41739350
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
 
LVL 76

Expert Comment

by:arnold
ID: 41739360
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739371
is the script executable? what do you get if you run:

ls -l /sysadm/scripts/Filemon.sh
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739374
did you get any error in crontab logs or user email?
0
 

Author Comment

by:Frog_1337
ID: 41739390
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 40

Expert Comment

by:omarfarid
ID: 41739395
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739399
if you run script with debug option:

sh -x /sysadm/scripts/Filemon.sh

what do you get?
0
 

Author Comment

by:Frog_1337
ID: 41739408
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739419
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739423
correction:

mailx -s 'Filesystem Report' myemailaddress < /tmp/filesysmon.txt
0
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41739439
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
 

Author Comment

by:Frog_1337
ID: 41739471
changed #!/bin/sh to #!/usr/bin/sh

tried both alternatives given both emails were empty
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41739477
You did not reply to some of my questions!
0
 

Author Comment

by:Frog_1337
ID: 41739482
Wins04 is the server name not sure what other question I missed
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
ID: 41739496
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
 

Assisted Solution

by:Frog_1337
Frog_1337 earned 0 total points
ID: 41739516
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
ID: 41739524
what is the shell used by root?
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
ID: 41739526
I can think of changing the crontab to:

30 12 * * * /usr/bin/sh /sysadm/scripts/Filemon.sh >> /sysadm/scripts/filemonerr.txt 2>&1
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 251 total points
ID: 41739627
Was there anything in the file when you tried my last suggestion?
0
 
LVL 4

Assisted Solution

by:Abhimanyu Suri
Abhimanyu Suri earned 83 total points
ID: 41739647
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
 

Author Comment

by:Frog_1337
ID: 41739713
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
ID: 41740119
but how the script was working fine from command line?
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 251 total points
ID: 41740233
Does your normal shell have an alias for df?
0
 

Author Comment

by:Frog_1337
ID: 41740457
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 41740487
Welcome
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41740568
You're welcome ;)
0
 

Author Comment

by:Frog_1337
ID: 41745673
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
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 251 total points
ID: 41745724
You change to grep for the percentage without the numbers, add an if to determine whether or not to send the mail message.
0
 
LVL 76

Assisted Solution

by:arnold
arnold earned 83 total points
ID: 41745729
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
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41745730
This should help, provided df gives you the desired output in % format , NO ALIASES ;)

df|egrep "(8[5-9]|9[0-9]|100)%"
0
 

Author Comment

by:Frog_1337
ID: 41753665
Awesome sorry for the delay dealing with health issues.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41753684
No problem at all, hope you're better now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now