Skip to content

Instantly share code, notes, and snippets.

View olivertappin's full-sized avatar
😁

Oliver Tappin olivertappin

😁
View GitHub Profile
@olivertappin
olivertappin / cookies-enabled.js
Created October 13, 2020 15:55
Detect whether cookies are enabled in the browser
function cookiesEnabled() {
if (navigator.cookieEnabled) {
return true;
}
document.cookie = 'cookietest=1';
let enabled = document.cookie.indexOf("cookietest=") !== -1;
document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
return enabled;
}
@olivertappin
olivertappin / extract.sh
Last active September 17, 2020 09:51
Extract a string from a commit message between two square brackets
# Example with a space after the colon
echo "bc3977f Some beautiful long commit message [skip: verification]" | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
verification
# Example with no space after the colon
echo "bc3977f Some beautiful long commit message [skip: verification]" | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
verification
# Working example from the last commit message
git log -1 --oneline | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
@olivertappin
olivertappin / docker-exec.sh
Created June 9, 2020 09:40
docker exec using the Docker Engine API with /var/run/docker.sock and curl
#!/bin/sh
# Why? In a containerised environment, you may need to run commands on other Docker containers
# within the Docker bridge network. For slim images such as Alpine-based distributions, you do
# not want the docker executable installed. Instead, you can simply pass the docker socket during
# runtime, and interact with it using cURL. The following functions allow you to easily interact
# with other containers, as you would using the native docker exec command.
# Example of mounting the docker socket:
#
@olivertappin
olivertappin / versioning-ranges.txt
Last active April 8, 2020 20:32
Semantic Versioning Ranges
For a simplistic view on what versioning ranges look like, see below:
Version Range
>=1.0 1.0.0 - *.*.*
>=1.0 <2.0 1.0.0 - 1.*.*
>=1.0 <1.1 || >=1.2 1.0.0 - 1.*.* || 1.2.0 - *.*.*
>=1.0 <1.1 1.0.0 - 1.*.*
>=1.0.0 <2.1 1.0.* - 2.0.*
>=1.0.0 <=2.1.0 1.0.0 - 2.1.0
~1.2 1.2.0 - 1.*.*
@olivertappin
olivertappin / install-xscreensaver.sh
Last active January 14, 2020 15:54
Install xscreensaver on Ubuntu
sudo apt-get install \
xscreensaver \
xscreensaver-gl-extra \
xscreensaver-data-extra \
xscreensaver-screensaver-bsod
# Or, via the preferred method:
# Look for the latest version from https://www.jwz.org/xscreensaver/download.html
sudo apt-get install \
xorg-dev \
@olivertappin
olivertappin / wordpress.conf
Created January 4, 2020 21:07
WordPress Nginx configuration
location / {
try_files $uri /index.php$is_args$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ /\. {
deny all;
}
@olivertappin
olivertappin / collect-cpu-stats
Created November 27, 2019 16:51
Collect CPU usage statistics for the top 5 processes over a 60 second period
#!/bin/bash
OUTPUT=~/stats.txt;
for i in {0..120};
do date >> $OUTPUT;
ps -Ao user,pid,pcpu,pmem,comm --sort=-pcpu | head -n6 >> $OUTPUT;
echo "" >> $OUTPUT;
sleep 0.5;
done
@olivertappin
olivertappin / show-blocking-transactions.sql
Created November 16, 2019 19:06
Run this query to check for a blocking transaction
SELECT
trx_w.trx_id AS waiting_trx_id,
trx_w.trx_mysql_thread_id AS waiting_process_id,
TIMESTAMPDIFF(SECOND, trx_w.trx_wait_started, CURRENT_TIMESTAMP) AS wait_time,
trx_w.trx_query AS waiting_query,
l.lock_table AS waiting_table_lock,
trx_b.trx_id AS blocking_trx_id,
trx_b.trx_mysql_thread_id AS blocking_process_id,
CONCAT(pl.user, '@', pl.host) AS blocking_user,
pl.command,
@olivertappin
olivertappin / database-table-counts.sql
Created May 10, 2019 13:43
Show counts of all table rows from a particular database using information_schema
SELECT
TABLE_NAME AS DATABASE_NAME,
FORMAT(TABLE_ROWS, 0) AS Rows
FROM information_schema.tables
WHERE
table_schema = 'DATABASE_NAME'
AND TABLE_ROWS > 0
;
@olivertappin
olivertappin / column-entry-creation.sql
Created March 26, 2019 13:12
Return the column entry for a SHOW CREATE TABLE (based on primary key or column name)
SELECT RTRIM(CONCAT(
'`',
COLUMN_NAME,
'`',
' ',
COLUMN_TYPE,
' ',
IF (COLLATION_NAME IS NULL, '', CONCAT('COLLATE ', COLLATION_NAME, ' ')),
IF (IS_NULLABLE = 'YES', '', 'NOT NULL '),
IF (COLUMN_DEFAULT IS NULL, '', CONCAT('DEFAULT ', COLUMN_DEFAULT, ' ')),