Mining Bitcoin with a 2012 MacBook Pro

So I started researching bitcoin mining and the feasibility of doing it with a laptop. The machine in question is a mid-2012 MacBook Pro. From what I have read, it’s not worth mining bitcoins (or any other semi-mature) cryptocurrencies on a MacBook Pro. I don’t think I’m up to the task of buying an ASIC miner, but that’s essentially what you should do now to even recoup what you spend on electricity.

I should’ve jumped on the mining cart back when i first heard about bitcoin. Maybe there’s a place for my MacBook Pro in the world of mining DogeCoin… 🙂

Screenshot Sharing Script for Mac OS

I recently wanted to move away from commercial screenshot sharing tools (I was using Jing), so I decided to create a script that takes screenshots, allows you to annotate them, then uploads them to my webhost. The host I use is Digital Ocean and I use sftp to upload the image. Here’s the script:

#! /bin/bash

filename=`date +%Y%m%d%I%M%S`;
filename+='_';
filename+=`openssl rand -hex 16`;
filename+='.png';
host="myhost.com"; # change to your host
domain="screens.host.com/subdir"; # this could be the same as host above for you

screencapture -i $filename;
printf "\nHit enter when you are done editing image in Preview. Don't forget to save it first (cmd+s).\n";
open $filename; #edit?
read go;

rsync $filename $host:/var/www/screenshots/subdir/;
echo "http://$domain/$filename" | pbcopy;

mv $filename ~/Documents/work/screenshots/;
osascript -e 'display notification "The image has been uploaded to your server." with title "Image Uploaded"';

That will take an interactive screenshot, open it in Preview so that you can edit and save it, then continue and upload it to your webhost when you press any key in the terminal. I have my terminal (iTerm2) set to hide with a keyboard combo (ctrl+space) so that I can tell it to get out of the way if I’m taking a screenshot of something else. I assigned the script to a bash alias, so all I have to to do is open a terminal and type my alias. It also copies the url to the screenshot to your clipboard using pbcopy so that you can just paste it when you want to share it.

Get a random file from a directory with the Linux terminal

Here’s how you can grab a random file from a directory using the GNU/Linux terminal:

ls | shuf -n 1

And perhaps you want to do something like open that file with a program:

your_program `ls | shuf -n 1`

I used this to do things like open a random image with feh from my pictures folder:

cd Pictures; feh `ls | shuf -n 1`

The magic here is the shuf command that will output n pseudorandom items from a collection that is fed to it.

Nginx and “413 Entity too large.”

That’s what she said! Okay, okay, here’s how to fix an “entity too large” error with nginx. Add this to your server block:

client_max_body_size 10M;

where 10M is the size of uploads you want to allow. The default is 2 MB, but as you know people have smartphones that take larger pictures than that now. Just remember you should probably resize them for web display until we all have better download speeds.

Changing the default session in Elementary OS

I installed Elementary OS a few months ago, but moved back to Ubuntu for some reason or another. A couple days ago I decided to give Elementary OS another try. Well, tonight I finally started missing openbox too much and decided to install it and give up on Elementary’s default desktop manager. Once I installed openbox, I logged out and noticed that the greeter didn’t have a way to change the session. A bit of googling came up with this:

“The desktop manager that ships with Luna is a highly modified version of lightdm. Some of the configuration options that might appear in a lightdm.conf file are not available. You will have to use another desktop manager in order to select different desktop environments or disable the guest account, among other things.”

That’s a funky thing to do… well I messed around and figured it out, but first try rebooting after installing another desktop environment or window manager – I’m not sure if that would’ve worked alone or what I did actually fixed it. If rebooting doesn’t work, try this:

  • Log out
  • Hit ctrl+alt+F1 to go to a tty prompt
  • enter sudo killall lightdm
  • enter sudo lightdm

Now, you should see a small gear icon above the password entry on the greeter. With that you can change which session to log in to.

Now that I think about it, it was probably just the reboot and that moderator didn’t know what he was talking about.

SQLite 3 and PHP – “unable to open database file”

While playing with SQLite 3 and PHP, I encountered an oddity: the SQLite database file must be writable AND be in a writable directory. So if you’re running SQLite commands from within a PHP web page, the directory your database is in and the database itself must be writable by your www user. With most GNU/Linux distros, this would take care of it:


sudo chown yourusername:www-data -R /path/to/db
sudo chmod 775 -R /path/to/db/yourdb.db

I wouldn’t advise working directly in your home folder or changing its permissions/ownership, though.

How to Move MySQL’s Data Directory

Sometimes it becomes necessary to move the location where MySQL stores databases. Either you have run out of room or something else has happened and you need to modify MySQL’s data directory. Here’s how to do it.

First, I would stop MySQL’s daemon if you aren’t running production databases currently (which I would suggest reading this entire guide and thinking of a different way of doing this if you are):

sudo service mysql stop

Next, move or copy the current database directory. I copied it because I had room and I wanted to play it safe. For Ubuntu, the default MySQL database directory is /var/lib/mysql, so to copy it I did:

sudo cp -R /var/lib/mysql /newdir/

where newdir is where I want all of the MySQL databases and data to be.

Then you’ll need to edit your MySQL configuration file. Edit /etc/mysql/my.cnf and change this line

datadir = /var/lib/mysql

to this

datadir = /newdir/mysql

and change newdir to your chosen directory.

Now, you’ll need to change part of AppArmor’s MySQL entry to reflect your new database directory. Edit /etc/apparmor.d/usr.sbin.mysqld and change

/var/lib/mysql/ r,
/var/lib/mysql/** rwk,

to

/newdir/mysql/ r,
/newdir/mysql/** rwk,

replacing newdir with your new directory. Now that you have changed everything, you can start MySQL again with sudo service mysql start.

Automatically Mount a Drive when Linux Boots

If you want to have a drive (more specifically, a partition) mount when a GNU/Linux distro boots, simply add it to your /etc/fstab file. The fstab for Ubuntu usually looks like this:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda1 during installation
UUID=e25e0070-6617-47d6-8ff1-77d873d3879c /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=62460a75-1863-4ee6-9c72-3b21aa64585d none            swap    sw              0       0

To mount the first partition (1) of the next drive (sdb) on [<35;124;17Mboot, change the file to this:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda1 during installation
UUID=e25e0070-6617-47d6-8ff1-77d873d3879c /               ext4    errors=remount-ro 0       1
/dev/sdb1   /media/mydrive  ext4    errors=remount-ro   0   1
# swap was on /dev/sda5 during installation
UUID=62460a75-1863-4ee6-9c72-3b21aa64585d none            swap    sw              0       0

Also, make sure you have created the mount directory and changed ownership to yourself:

sudo mkdir /media/mydrive
sudo chown -R yourusername /media/mydrive

Create a Windows 8 Bootable USB Drive with Mac OS

Mac OS X 10.8 (Mountain Lion) makes it relatively easy to create a bootable USB drive for installing Windows 8. You don’t have to be installing Windows 8 to the Mac, it will work on any computer that can boot from USB. I used Mac OS to prepare a bootable USB to install Windows 8 on a regular PC laptop. You will need the following:

  • USB drive that is at least 4 GB
  • Windows 8 ISO

To create the drive, open Bootcamp Assistant from the applications in Mac OS. Uncheck the box that says something about downloading Windows 7 updates, then continue. On the next screen, select the drive that you want to install the Windows 8 installer to and select the Windows 8 ISO file. Once you click next, Mac OS will format the drive and create a bootable Windows 8 USB drive with it.

If you need to create an ISO from a Windows 8 disk, this is how you would do that (from superuser.com):

“Disk Utility – The on-screen prompts will guide you, but it will by default create a .dmg, which is an OS X-specific file format. Disk Utility will also create an ISO (.cdr extension) if you select the “CD/DVD Master” option before creation the image. Rename the extension (to .iso) after creation if needed.”

Adding a user to sudoers file

“username is not in the sudoers file. This incident will be reported.”

Aw. Well if you have root access, here’s how to add your user to the sudoers file. This will allow you to simply use sudo instead of su and logging in as root.

su
echo 'username ALL=(ALL) ALL' >> /etc/sudoers

The first command will ask you for the root password. The second command adds username to the sudoers file. Replace username with your actual username.