Skip to content

Instantly share code, notes, and snippets.

@alessandro-fazzi
Created July 18, 2012 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessandro-fazzi/3136137 to your computer and use it in GitHub Desktop.
Save alessandro-fazzi/3136137 to your computer and use it in GitHub Desktop.
Unix notes. Personal notes about intresting sysadmin's operations
# Text color variables
txtred=$(tput setaf 1) #Red
txtgre=$(tput setaf 2) # Green
txtyel=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtbol=$(tput bold) # Bold
txtres=$(tput sgr0) # Reset
# Helper feedback functions
function info() {
echo "${txtbol} * ${1}${txtres}"
}
function success() {
echo "${txtbol}${txtgre} ** ${1}${txtres}"
}
function warning() {
echo "${txtbol}${txtyel} *** ${1}${txtres}"
}
function error() {
echo "${txtbol}${txtred} **** ${1}${txtres}"
}
function question() {
echo -n "${txtbol}${txtblu} ? ${1}?${txtres} "
}
# Just our root's .bashrc emtry to colorize the prompt
## Fancy prompt
red='\[\e[0;31m\]'
RED='\[\e[1;31m\]'
blue='\[\e[0;34m\]'
BLUE='\[\e[1;34m\]'
cyan='\[\e[0;36m\]'
CYAN='\[\e[1;36m\]'
black='\[\e[0;30m\]'
BLACK='\[\e[1;30m\]'
green='\[\e[0;32m\]'
GREEN='\[\e[1;32m\]'
yellow='\[\e[0;33m\]'
YELLOW='\[\e[1;33m\]'
magenta='\[\e[0;35m\]'
MAGENTA='\[\e[1;35m\]'
white='\[\e[0;37m\]'
WHITE='\[\e[1;37m\]'
NC='\[\e[0m\]' # No Color
#
PS1="${RED}\h${NC} ${BLUE}\w${NC} ${RED}# ${NC} "
#Obtain total occupied space of certain file types found by extensions
find . -regextype posix-extended -regex ".*(mp3|avi)$" -print0 | xargs -0 du -hc | tail -n1
#! /bin/bash
# Nice way to change strings inside .git/config to fix e.g. origine massively.
# Tested cross compatible with Linux and OSX thanks to Arkham's trick to use perl instead of sed
set -e
SED_EXPR='s|url = ssh://(\w*@)?code.welaika.com/var/git-repos/(.*)\.git|url = git\@code.welaika.com:$2|'
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
for file in `find . -name "config" -type f`; do
diff $file <(perl -p -e "$SED_EXPR" "$file") || (
echo
echo -n "Replace this file? (y/n): "
read answer
if [[ $answer == "y" ]]; then
perl -pi -e "$SED_EXPR" "$file"
fi
echo
)
done
# in_array helper bash function
# I've copied it from http://mykospark.net/tag/in_array/
# One beer for Mike.
# in_array (string)needle (array)haystack
function in_array() {
local x
ENTRY=$1
shift 1
ARRAY=( "$@" )
[ -z "${ARRAY}" ] && return 1
[ -z "${ENTRY}" ] && return 1
for x in ${ARRAY[@]}; do
[ "${x}" == "${ENTRY}" ] && return 0
done
return 1
}
#!/bin/bash
# This is a script to check and fix openbasedir restrictions for
# each domain hosted on a MediaTemple Grid.
# Not guaranteed to work for everyone, but it should. User is intended
# to have a good knowledge of what is he doing.
# Config gridID before use.
gridID=''
# No more config below
# Text color variables
txtred=$(tput setaf 1) #Red
txtgre=$(tput setaf 2) # Green
txtyel=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtbol=$(tput bold) # Bold
txtres=$(tput sgr0) # Reset
# Helper feedback functions
function info() {
echo "${txtbol} * ${1}${txtres}"
}
function success() {
echo "${txtbol}${txtgre} ** ${1}${txtres}"
}
function warning() {
echo "${txtbol}${txtyel} *** ${1}${txtres}"
}
function error() {
echo "${txtbol}${txtred} **** ${1}${txtres}"
}
function question() {
echo -n "${txtbol}${txtblu} ? ${1}?${txtres} "
}
cd /home/$gridID/domains
for i in `ls`; do
if [[ `cat $i/.htaccess | grep open_basedir | grep $i` ]]; then
success "$i OK"
else
info '################'
info "Questo è il file .htaccess per il dominio $i"
info ''
info "$(cat $i/.htaccess)"
info '################'
question "$i non sembra ok. Vuoi continuare? [Y/n]"
read answer
read -r -d '' text<<EOT
# BEGIN php selector
<IfModule !mod_fcgid.c>
AddHandler php-stable .php
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files *.php>
Options +ExecCGI
</Files>
</IfModule>
# END php selector
#BEGIN_GSBASEDIR
Options +FollowSymLinks
php_value open_basedir /home/$gridID/domains/${i}/html:/tmp:/home/$gridID/data/tmp:/usr/local:/etc/apache2/gs-bin
#END_GSBASEDIR
EOT
if [[ $answer == '' || $answer == 'Y' || $answer == 'y' ]]; then
echo -e "$text" > ${i}/.htaccess
fi
fi
done
mycmd="smbd";
for i in `ps ax -o rss,command | grep $mycmd | awk '{ print $1 }'`; do var=$(($var+$i)); done; var=$(($var/1024)); echo "$var MB";
#Erase from postfix queue mails from specific sender
postqueue -p | awk 'BEGIN { RS = "" }{ if ( $7 ~ /[mail.com]/ ) print $1 }' | tr -d "*" | postsuper -d -
#!/bin/bash
set -e
PID=""
STOPCMD=""
STARTCMD=""
APPPATH="" #some times you have to launch commands within the app dir (rvm?)
function statusok() {
if [[ $? != 0 ]]; then
logger -t [$(basename $0)] "Error occurred"
exit 1
else
return 0
fi
}
cd $APPPATH
if [[ -e $PID ]]; then
if [[ `ps axo pid | grep $(cat $PID)` ]]; then
logger -t [$(basename $0)] "PID exists - assume process is running"
exit 0
else
$STOPCMD
statusok && logger -t [$(basename $0)] "Stopping process"
$STARTCMD
statusok && logger -t [$(basename $0)] "Starting process"
fi
else
$STARTCMD
statusok && logger -t [$(basename $0)] "Starting process"
fi
# Generate a random password
# $1 = number of characters; defaults to 32
# $2 = include special characters; 1 = yes, 0 = no; defaults to 1
function randpass() {
[ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
echo
}
# Good practice to trap exit signals from scirpt with associated actions
# In the example removing temp/pid file
trap 'rm -f "$Tmp" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment