lunes, 24 de octubre de 2016

disk space monitor

#!/bin/bash
space=$(df -h | awk '{print $5}' | awk 'NR==2')

disk=${space:0:2}

count=100
if [ $disk -gt 80 ]
then
  echo "$(df -h )
 $(date)" | mail -s " Warning disk is full " ambiorixg12@gmail.com

else

echo  "Disk space OK  $(df -h )
 $(date)"

echo "$(df -h )
 $(date)" | mail -s " Disk space report " ambiorixg12@gmail.com


fi



@daily /root/monitor.sh > /root/disk-results.txt 2> /root/disk-errror.tx

martes, 20 de septiembre de 2016

service monitor

#!/bin/bash
 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export DISPLAY=:0.0


servicepid=`ps  aux | grep -i $1 | grep -v grep | awk '{print $2}' `
servicename=`ps  aux | grep -i $1 | grep -v grep | awk '{print $11}' `

if [ "$servicepid" = "" ]; then
echo process not running
#/root/calltxtfile.sh
else
echo  "$servicename running on date  $(date)">>/root/log.log
fi





running the script
./servicemonitor.sh  asterisk

lunes, 11 de enero de 2016

bash checking vm

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

vmuser=1234
outnum=18097143489
trunk=rapidvox
echo $vmuser
context=internal
vmnum=$(asterisk -x " voicemail show users" | grep -i $vmuser | awk '{print $5}' | awk 'NR==2')

#`asterisk -x "originate SIP/$1 extension $2@$context"`
if [ $vmnum -eq 0 ]
then
echo "no new voicemail, number of voicemail is equal to $vmnum $(date)"

else
echo "yo got $vmnum voicemail $(date)"
#`asterisk -x "originate SIP/$trunk/$outnum extension s@$context"`

fi

Bash: Meaning of “[: too many arguments


I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.
The error:
-bash: [: too many arguments
Google-friendly version: bash open square bracket too many arguments.
Context: an if condition in single square brackets with a simple comparison operator like equals, greater than etc, for example:
VARIABLE=$(/some/command);
if [ $VARIABLE == 0 ]; then
  # some action
fi 
shareimprove this question
1 
Where is the code that produced this specific error? – Anderson Green Jun 17 '13 at 22:39

2 Answers

up vote90down voteaccepted
If your $VARIABLE is a string containing spaces or other special characters, and single square brackets are used (which is a shortcut for the test command), then the string may be split out into multiple words. Each of these is treated as a separate argument.
So that one variable is split out into many arguments.
The same will be true for any function call that puts down a string containing spaces or other special characters.

Easy fix

Wrap the variable output in double quotes, forcing it to stay as one string (therefore one argument). For example,
VARIABLE=$(/some/command);
if [ "$VARIABLE" == 0 ]; then
  # some action
fi 

An alternate fix is to use double square brackets (which is a shortcut for the new test command).
This exists only in bash (and apparently korn and zsh) however, and so may not be compatible with default shells called by /bin/sh etc. This means on some systems, for example, it might work from the console but not from cron, depending on how everything is configured.
It would look like this:
VARIABLE=$(/some/command);
if [[ $VARIABLE == 0 ]]; then
  # some action
fi 
http://stackoverflow.com/questions/13781216/bash-meaning-of-too-many-arguments-error-from-if-square-brackets



echo "*file 2" | grep -o ^. prints *.
Since you have a command substitution outside double quotes, it undergoes globbing (a.k.a. wildcard matching a.k.a. filename generation) and word splitting. If the current directory is not empty, *expands to the list of files in the current directory. Each file becomes one token in the [ command, which is highly likely to be a syntax error.
The problem is that you didn't use double quotes around the command substitution. Always use double quotes around variable and command substitutions unless you have a good reason to omit them.
if [ "$(echo "*file 2" | grep -o ^.)" = '.' ]
http://unix.stackexchange.com/questions/162616/why-am-i-getting-too-many-arguments

sábado, 28 de noviembre de 2015

adding a path to list

export PATH=$PATH:/root/

domingo, 15 de noviembre de 2015

Monitor UNIX / Linux Server Disk Space with Shell Script


Shell script to monitor or watch the disk space and send an email alert if the (free avilable) percentage of space is >= 90%
  1. #!/bin/sh
  2. # Shell script to monitor or watch the disk space
  3. # It will send an email to $ADMIN, if the (free avilable) percentage
  4. # of space is >= 90%
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # -------------------------------------------------------------------------
  9. # This script is part of nixCraft shell script collection (NSSC)
  10. # Visit http://bash.cyberciti.biz/ for more information.
  11. # ----------------------------------------------------------------------
  12. # Linux shell script to watch disk space (should work on other UNIX oses )
  13. # SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
  14. # set admin email so that you can get email
  15. ADMIN="me@somewher.com"
  16. # set alert level 90% is default
  17. ALERT=90
  18. df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  19. do
  20. #echo $output
  21. usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
  22. partition=$(echo $output | awk '{ print $2 }' )
  23. if [ $usep -ge $ALERT ]; then
  24. echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
  25. mail -s "Alert: Almost out of disk space $usep" $ADMIN
  26. fi
  27. done

viernes, 28 de agosto de 2015

Chapter 3. Special Characters

http://tldp.org/LDP/abs/html/special-chars.html