Archive for the ‘BASH Scripting’ Category.

cron and its limited enviroment….

If you ever see this from a crron script :

$FILE_RUN_FROM_CRON: line $LINE: $COMMAND: command not found
Error: (CLI:043) Invalid shell command syntax.

 

 

Then you likely:

  • Forgot to setup a proper PATH in your script
  • You declared a variable but didnt use it or the file isnt in the setup PATH (ie set VAR1=/usr/local/bin/crazybinary but then just used¬† crazybinary in the script)
  • messed up something in a /etc/profile.d/ file
  • Likely more but ….

I run across this from time to time due to typos I make and thought the internet at large might benefit. Don’t forget cron has a limited environment by default ! Lots of ways on the net to fix this but this error was a little more hidden so I thought I would help it get some google love! For those that wanted to know about how different the cron environment is here is a (near) default RHEL6 environment:

SHELL=/bin/sh
MAILTO=
USER=root
PATH=/usr/bin:/bin
PWD=/root
LANG=en_US.UTF-8
SHLVL=1
HOME=/root
LOGNAME=root
_=/bin/env

Quite Spartan !

Quick CLI Hack to set writecache on 3w-9xxx 9650SE on the cli

First off this hack requires you have the tw_cli tool installed. If so just modify this to suite your needs and have fun. I ran into the need for this after I noticed that the following log messages :


Jan 13 11:09:44 pbnj kernel: 3w-9xxx: scsi0: AEN: INFO (0x04:0x0055): Battery charging started:.
Jan 13 11:09:45 pbnj kernel: 3w-9xxx: scsi0: AEN: INFO (0x04:0x0056): Battery charging completed:.

would cause the write cache to get turned off. Likely a GUI way or flag to trip but I like a little CLI challenge on my lunch break ;)

WARNING: MAKE SURE YOU HAVE A BATTERY HOOKED UP AND CHARGED BEFORE DOING THIS!

Not using a battery with write cache on is dangerous…sometimes silly..and sometimes down right deadly to your data. Use with caution! I am not responsible if you use this and it harms your system in anyway!

UPDATE : A more powerful version of this code now lives at My Gitlab Page for this project . I am going to be updating there moving forward.


TWCLI=/usr/sbin/tw_cli
SYSLOG="/usr/bin/logger -t LSI"
CONTROLLER=$(tw_cli show | awk '/c./ {print $1}')
for i in $($TWCLI /$CONTROLLER show unitstatus | egrep -o 'u[0-9]'); do
    case $($TWCLI /$CONTROLLER/$i show wrcache | cut -d= -f2 | sed 's/^[ \t]*//;/^$/d') in
	on)
	$SYSLOG "WriteCache is on for /c2/$i"
	;;
	off)
	$SYSLOG "WriteCache is off for /c2/$i ..turning on"
	$TWCLI /$CONTROLLER/$i set wrcache=on quiet
	;;
	*)
	$SYSLOG "WriteCache or script is fubar please fix"
	;;
    esac
done

Quick BASH check for slow DNS queries

This little script (sorry for the formatting..I really need to clean up these scripts in worldpress) can start the debug process for slow DNS queries. It could also be used, witn a little work, to populate a graph if you see frequent issues.


#!/bin/bash
NS=$YOUR_NAME_SERVER_HERE
DOMAIN=Domain your testing with
for i in {1..100} ; do 
   dig @$NS $DOMAIN | awk -F: '/Query/ {print $2}';
   sleep 1 ; 
done

A server under load will be all over the place. I recently helped someone with this issue where a nameserver was going into swap and was causing VERY slow (900+ ms) NS lookups. I start with a domain the server is auth for as that should be fastest and have the lowest network load but if you dont know any be prepared for a slow response or two as the server populates its cache.
Here is what I saw from a test on a non-auth domain for a server that is local:

151 msec
0 msec
0 msec
1 msec

and for a domain that the server is auth for…

0 msec
0 msec

and against a remote DNS server at google…

101 msec
26 msec
27 msec
25 msec
24 msec

I have begun building on this to help troubleshoot further as to where the latency exists. Just a quick 5 min hack I did that helped someone that might help someone else.

Finding who is using dovecot’s imap process in the process table

Ever want to know who has what imap-login ? Then run something along these lines

for i in $(ps -ef | awk '/[i]map$/ {print $2}'); do
      echo "$(lsof -p $i | awk '/cwd/ {print $9}') is handled by process id $i" ;
done

Which returns something along these lines (names changed to protect the innocent ;) .

$PATHTOVPOPMAIL/domains/$DOMAIN/$USER is handled by process id 1277

Just a quick lunch time hack that might help some errant google searcher.

Quick IPTABLES Connections Hack

Did a quick hack to see whats going on with a Linux IPTABLES firewall connection wise.

iptstate -s | awk ‘{print $3,$2,$1}’| cut -d”:” -f1-2 | uniq -c | sort -g

This shows you

# number of connections | Protocol |  Dest_IP:PORT | Source IP

I will add this to my bash wiki section in case it can help anyone.