Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Forked from mpneuried/cron.sh
Created July 9, 2018 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monkeymonk/cd6da7020a3ac4774e295a8d241528d6 to your computer and use it in GitHub Desktop.
Save monkeymonk/cd6da7020a3ac4774e295a8d241528d6 to your computer and use it in GitHub Desktop.
bash helpers
# CRON: crontab header to run node and grunt within a cron job
#!/bin/sh
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# GIT: rename a git branch
git branch -m oldname newname
git push origin newname
git push origin :oldname
# GIT: remove a remote tag
git tag -d __tagname__
git push origin :refs/tags/__tagname__
# GIT: create github repo from folder
cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master
# GIT: reset to head
git fetch origin
git reset --hard origin/master
# GIT: change the latest commit message
git commit --amend -m "New commit message"
# GIT: compare two branches
git diff --name-status master..branchName
# NETWORK: find a process listening to a specific port
sudo lsof -i TCP:$PORT | grep LISTEN
# NETWORK: limit network conncetion
ipfw pipe 1 config bw 64KByte/s # Pipe erstellen, die nur 64kB/s durchlässt
ipfw add 1 pipe 1 src-port 80 # Pipe an den Sourceport 80 binden
ipfw delete 1 # Pipe wieder löschen
#NETWORK: do a port scan via netcat
nc -zv localhost 3000-3999
# NODE: run grunt with debugger
node-debug $(which grunt) task
# NODE.js: fix LIFECYLE Error
# ld: library not found for -lgcc_s.10.5
# clang: error: linker command failed with exit code 1
cd /usr/local/lib
sudo ln -s ../../lib/libSystem.B.dylib libgcc_s.10.5.dylib
# Start node with a defined obj-memory limit.
# This means a limit for the generated data inside the process.
# You have to add the used memory of the runtime and code to get the real memory usage.
# Default is 1,4GB - https://github.com/nodejs/node-v0.x-archive/wiki/FAQ#what-is-the-memory-limit-on-a-node-process
# e.g.: 100b
node --max-old-space-size=100 index.js
#NPM list the latest versions of dependencies
echo ".dependencies"; jq ".dependencies|keys|.[]" package.json -r | xargs -I{} npm info {} -j | jq "{(.name):.version}" -c -r;echo ".devDependencies"; jq ".devDependencies|keys|.[]" package.json -r | xargs -I{} npm info {} -j | jq "{(.name):.version}" -c -r;
# REDIS: Redis delete by wildcard
~/local/redis/redis-cli KEYS "prefix:*" | xargs ~/local/redis/redis-cli DEL
# GM (GraphicsMagick): place a image inside another image.
# place it to position x:200px, y:200px with a width of 50px
gm convert -page +0+0 ./source.jpg -resize 50 -page +200+200 ./placement.png -mosaic ./output.jpg
#Docker: set machine ip to env
export DOCKERLOCALHOST=`docker-machine ip default`
# MISC: convert all m4a files to mp3 for all subfolders (brew packages: ffmpeg, rename)
# convert to mp3
find . -name "*.m4a" -exec ffmpeg -i {} -acodec libmp3lame -ab 320k "{}.mp3" \;
# rename
find . -name "*.m4a.mp3" -exec rename -s ".m4a.mp3" ".mp3" {} \;
# delete m4a files
find . -name "*.m4a" -exec rm {} \;
# pipe to multiple copmmands
echo "test" | tee >( cat - > testa.txt ) | cat - > testb.txt
# write the output of multiple commands to a file
{ echo "line 1"; echo "line 2"; ls } > cmb.txt
# query a prometheus metric every 60 seconds and generate a csv
echo "first_metric,second_metric" > ~/Downloads/my_prom_log.log; \
while true; do \
curl -s http://my-service/metrics | \
grep "^first_metric\|^second_metric" | \
awk -F " " '{print $2}' | \
paste -s -d, - >> ~/Downloads/my_prom_log.log; \
sleep 60; done
# Using SCREEN
# create a session with name
screen -S sessionname
# detach from session
# > Ctrl+A,D
# list sessions
screen -ls
# reconnect to session (id and name see screen -ls)
screen -r sessionid.sessioname
# SYSTEM: open a file as admin with a special app
sudo open -a /Applications/Utilities/Console.app/ /usr/local/mysql/data/Mathiass-iMac.log
# SYSTEM: Create a symbolic link
ln -s /Path/to/original/folder New-folder-name
# SYSTEM: show long running clock
clear; while true; do echo -e \\b\\b\\b\\b\\b\\b\\b\\b`date +%T`\\c ; sleep 1; done
# SYSTEM: Set ENV var
export FOO=42
#SYSTEM: Find content in files of folder
grep -rinHo "my string"
# SYSTEM: copy files by regex pattern. E.g. copy only jpegs with odd numbers
find . -type f | grep -i "[13579].jpg" | xargs -I{} cp {} ../test_cp/{}
find ./src/folder -type f | grep -i "regexp" | xargs -I{} cp {} ../target/folder/{}
# SYSTEM: find big folders
du --max-depth=7 /* | sort -n
# SYSTEM un tar files
tar -zxvf yourfile.tar.gz
# SYSTEM: Run a script (eg.g imagemagick convert ) for a bunch of files by regex
PREFIX=$2
SIZE=$1
for main in `seq 1 36`; do
ls | grep -e "Uebung$main[A-C]*.jpg" | xargs echo | xargs -I "{}" sh -c "convert -delay 75 -resize $SIZE {} $PREFIX$main.gif"
echo "$PREFIX$main.gif created"
done;
# list the file/folder sizes
du -sh $PWD/*
# meassure time a script uses
START_TIME=$SECONDS;
sleep 3;
ELAPSED_TIME=$(($SECONDS - $START_TIME));
echo "$ELAPSED_TIME sek"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment