Sunday, November 6, 2011

Bash trick: last command shortcut

In bash, two consecutive exclamation marks (!!) stand for 'the previous command'. One instance where this could be particularly useful is when you forget the sudo command (happens to me often):
[jorge@flamingo media]$ mkdir test
mkdir: cannot create directory `test': Permission denied
(Ah, yes, I forgot the sudo!)
[jorge@flamingo media]$ sudo !!
sudo mkdir test
Password: 

Friday, November 4, 2011

Dropbox does not connect on startup

I have issues with Dropbox on my laptop running arch linux. If the internet connection is lost, uppon
reconnection Dropobox fails to reconnect and its status stays forever to "Connecting". I wrote a script
that checks if there is connection to the internet and if so, it restarts the dropbox service if hung. It has
to be run on startup in the background.

#!/bin/bash

# While there is a connection, the script checks every 60 secs if 
# the dropbox status is "Dropbox isn't running" or "Connecting",
# and if so, it reconnects the service. Could start from .xinitrc.

while true; do
    ping -c 1 www.google.com 1>/dev/null 2>/dev/null
    if [ "$?" = 0 ]; then
        dropbox status | egrep 'isn|Connecting'>/dev/null
        if [ "$?" = 0 ]; then
            dropbox stop
            dropbox start
        fi
     fi
    sleep 2
done

To make this work you have to run the above script on startup. For instance, you could do the following:
  • Put the above lines in a file called checkdropboxstatus.sh. 
  • Put that file in you /home/yourusername/.config/autostart/ folder.
  • Go to that folder and change the permissions to executable
    cd /home/yourusername/.config/autostart
    chmod +x checkdropbox.sh
Now dropbox shouldn't hung anymore.