B.A.Sc in Software Engineering, M.Sc. and Ph.D. in Computer Science; 20 years: federal government, private sector and R & D software, design and testing.
A collection of code snippets of sometimes rarely used, or perhaps difficult to remember tasks, commands and code snippets.
Delete the "demo_maintenance" branch on the "origin" remote server.
git push origin :demo_maintenance # deletes the remote branch git branch -d demo_maintenance # deletes the local branch
If you need to 'force' the delete on your local machine, use -D
git branch -D demo_maintenance # deletes the local branch
First locat the branches, then checkout the one you want (e.g. origin/demo1)
git branch -r git checkout --track -b demo1 origin/demo1
If you committing a git change locally, you can undo it using the following command:
git reset --soft HEAD^
If you already committed the change, then you need to do hard resets:
git reset --hard HEAD~1 git push -f
ssh-keygen -t rsa
dscacheutil -flushcache
On newer Macs, somethings this works too.
sudo killall -HUP mDNSResponder
When dealing with CSV, you need to get your encodings correct.Excel will typically export to ISO-8859-1. This is not good, and should be converted to UTF-8 to support non english files.
To check the encoding of a file
file -I original_file.csv
To convert between encodings (a few common examples)
iconv --from-code=ISO-8859-1 --to-code=UTF-8 original_file.csv > new_file.utf8.csv iconv --from-code=MacRoman --to-code=UTF-8 original_file.csv > new_file.utf8.csv
When exporting files to be read within Excel, you will need to convert the CSV based on the target platform.
For MAC
iconv --from-code=UTF-8 --to-code=MacRoman downloaded_file.csv > downloaded_file_mac.excel.csv
For PC (untested)
iconv --from-code=UTF-8 --to-code=Windows-1252 downloaded_file.csv > downloaded_file_windows.excel.csv
For simple scripts, just pass them via a quoted string.
ssh name@server.com 'ls -la'
For more complicated scripts, put them in a local file and stream them to the server.
ssh name@server.com 'bash -s' < local_script.sh
# enable the ftp server sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist #disable the server when you are done sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist
grep -r "some words reward" /path/tp/look/*
The following should owrk on your Mac OS X
sudo find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
For a linux (tested on Ubuntu), the $9 most liley needs to be an $8
sudo find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
Find which process is listening on a certain port
lsof -i :12345 # for port 12345
The following will get you SSH access back
mount -n -o remount,rw / mkdir /var/run/network /etc/init.d/networking restart mkdir /dev/pts mount /dev/pts /etc/init.d/ssh restart
Next to start-up the services you care about
sudo /etc/init.d/apache2 start sudo /etc/init.d/cron start sudo service mysql start
Should only be done on a dev machine:
rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app
Compress a directly into a tar.gz file:
tar cvzf myapp.tar.gz myapp/
Uncompress that file back into a directory:
tar zxfv myapp.tar.gz
Uncompress a gz zip (no tar):
gzcat x.txt.gz >x.txt gunzip -c x.txt.gz >x.txt
Don't forget to clear your sessions every once in a while
rake db:sessions:clear
$start_time = microtime(true); $start_memory = memory_get_usage(); // INSERT CODE HERE $end_memory = memory_get_usage(); $end_time = microtime(true); print_r(array( 'memory (Mb)' => ($end_memory - $start_memory) / (1024 * 1024), 'time (s)' => $end_time - $start_time ));
$memcache = new Memcache(); $memcache->connect('localhost', 11211); function doSomethingSlow() { sleep(5); return "foobar"; } $name = $memcache->get('name'); if (!$name) { $name = doSomethingSlow(); $memcache->set('name',$name); } echo $name;
A simple implementation of a scratch card program using JQuery.
Remove all old sessions, and then optimize the table.
DELETE FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL 1 DAY); optimize table sessions;
require "yaml" File.open("./example.yml", 'w') {|f| f.write(" workbook: 5 sheet: 10 header_index: 5 headers: - hello - hi - bye ") } data = YAML.load_file("./example.yml") puts "OLD DATA: #{data}" data["headers"] = [ "Hello", "Hi", "Bye" ] File.open("./example.yml", 'w') {|f| f.write(data.to_yaml) } data = YAML.load_file("./example.yml") puts "NEW DATA: #{data}"
mysql -u myname -pmypassword -D mydb -e "show tables"
SHOW GRANTS FOR deployer # ALL ACCESS GRANT ALL ON myshop.* TO 'deployer'@'%' IDENTIFIED BY 'pass' ; # SELECT ONLY, and filtered by IP GRANT SELECT ON myshop.* TO deployer@'192.168.1.%' IDENTIFIED BY 'pass'; # Or by domain name GRANT SELECT ON myshop.* TO deployer@'webapp.myserver.com' IDENTIFIED BY 'pass'; FLUSH PRIVILEGES;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'deployer'@'%';
CREATE USER myuser WITH ENCRYPTED PASSWORD 'ppp'; GRANT USAGE ON SCHEMA public to myuser; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO myuser; GRANT CONNECT ON DATABASE mydb to myuser; \c mydb GRANT USAGE ON SCHEMA public to myuser; GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO myuser; GRANT SELECT ON ALL TABLES IN SCHEMA public TO myuser;
# Locate defunct processes ps -A | grep defunct # Get the parent ID (UID PID PPID ...) ps -ef | grep defunct | more # for example... deployer 2707 2698 0 2012 ? 00:00:03 [unicorn_rails]deployer 2710 2698 0 2012 ? 00:00:04 [unicorn_rails] # Kill the parent PID (PPID) kill -9 2698
Downloaded ZIP files are typically quarantined on Mac OSX, so you might run into forbidden issues when extracting something like a wordpress site.
$ xattr myapp.zip com.apple.quarantine xattr -d com.apple.quarantine myapp.zip # Or, if you already extracted it xattr -rd com.apple.quarantine ./myapp
A longer discussion at ewal.net.
sudo pmset -a standbydelay 86400 # only standby after 24 hours
Bug with current version that will cause the process to not exit properly:
brew uninstall wkhtmltopdf cd /usr/local/Library/Formula/ git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb brew install wkhtmltopdf wkhtmltopdf --version | grep 0.9.9
Ticket was described here, but now the site (and history) have for wkhtmltopdf.org have moved. Thank you to wearepandr
When running CHEF, it needs root access, so if you are using something like chef-solo, and you like automated scripts, then here is how grant that access. PLEASE NOTE, THIS IS VERY UNSAFE, to avoid try, consider using CHEF BOOTSTRAP.
$ mkdir -p /root/.ssh $ chmod 700 /root/.ssh $ chmod 600 /roiot/.ssh/authorized_keys $ restorecon -R -v /root/.ssh
GRANT ALL PRIVILEGES ON DATABASE @DATABASE_NAME@ TO @NEW_USERNAME@; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO @NEW_USERNAME@;
For example, changing from the 'postgres' user to the 'deployer' user.
GRANT ALL PRIVILEGES ON DATABASE my_app TO deployer; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO deployer;
If you receive warnings about should and either are not ready to change, or don't want to, here is how you can silence the warnings.
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead.
Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead.
RSpec.configure do |config| config.mock_with :rspec do |c| c.syntax = [:should, :expect] end config.expect_with :rspec do |c| c.syntax = [:should, :expect] end end