Skip to content

Instantly share code, notes, and snippets.

@gipi
Last active November 10, 2017 12:27
Show Gist options
  • Save gipi/10984494 to your computer and use it in GitHub Desktop.
Save gipi/10984494 to your computer and use it in GitHub Desktop.
#bash
 # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

check if there output

TWOTWOTWOTWO=$(netstat -ltp 2>/dev/null | grep ':2222')
test -n "${TWOTWOTWOTWO}" && echo 'port 2222 already assigned'
#!/bin/bash
# http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/
readonly PROGNAME=$(basename "$0")
readonly LOCKFILE_DIR=/tmp
readonly LOCK_FD=200
lock() {
local prefix=$1
local fd=${2:-$LOCK_FD}
local lock_file=$LOCKFILE_DIR/$prefix.lock
# create lock file
eval "exec $fd>$lock_file"
# acquier the lock
flock -n $fd \
&& return 0 \
|| return 1
}
eexit() {
local error_str="$@"
echo $error_str
exit 1
}
main() {
lock $PROGNAME \
|| eexit "Only one instance of $PROGNAME can run at one time."
do_A
do_B
}
main
#!/bin/bash
# <http://kirk.webfinish.com/?p=85>
function debug_log () {
logger -t $0 -i -s -- $USER : $BASH_COMMAND
}
trap debug_log DEBUG
echo "This is a trap"
echo "This is another trap"
exit
start() {
echo -n 'starting...'
test -f ${PID_FILE} && {
echo 'process already running ('${PID_FILE}')'
exit 1
}
${LOOP_BINARY} &
echo $! > ${PID_FILE}
echo ' ok'
}
stop () {
PID=`cat ${PID_FILE}`
echo -n 'killing pid '$PID'...'
# http://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes
PGID=$(ps opgid= "$PID" | tr -d ' ')
kill -- -${PGID} && rm ${PID_FILE}
echo ' ok'
}
#!/bin/bash
# syntax: $0 /src/dir dstbox1:[/dst/dir] [ dstbox2:[/dst/dir] dstbox3:[/dst/dir] ... ]
# using recursion (ssh version)
# from <http://backreference.org/2014/05/18/poor-mans-directory-tree-replication/>
do_sshtar(){
local dstbox=${1%:*} dstpath=${1#*:}
[ -n "$dstpath" ] || dstpath=$srcpath
shift
if [ $# -eq 0 ]; then
# end recursion
ssh "$dstbox" "tar -C '$dstpath' -xzvf -"
else
# send data to "current" $dstbox and recurse
tee >(ssh "$dstbox" "tar -C '$dstpath' -xzvf -") >(do_sshtar "$@") >/dev/null
fi
}
srcpath=$1
shift
tar -C "$srcpath" -czvf - . | do_sshtar "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment