October 2014
M T W T F S S
« Mar    
 12345
6789101112
13141516171819
20212223242526
2728293031  
134

Refs

Categories

Archives

profile for slm on Stack Exchange, a network of free, community-driven Q&A sites

[one-liner]: Script to Cleanly Start Dropbox During a boot sequence on Linux (Fedora & CentOS)

Background

I’m an avid user of Dropbox. I run it on all my Linux and Windows systems, and it simply just works. However, one annoying thing that I’ve just dealt with for a while now on Linux, is when I reboot my laptop, Dropbox, doesn’t come up correctly due to the wireless network not being up just yet. I should mention here, that I’m using NetworkManager to manage my networks.

For whatever reason, this seems to hang Dropbox up and even after NetworkManager gets around to connecting to a wireless network, I still have to restart Dropbox.

Solution

So after dealing with this for over a year, I figured it was a good time to address this. My approach was to simply wrap the startup of Dropbox via a shell script, so that Dropbox wouldn’t start until my wireless network was available. It only runs during boot-up, after starting Dropbox, it simply exits.

Here’s my script, called start_dropbox.bash:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
 
# SCRIPT:      start_dropbox.sh
# DESCRIPTION: Guards dropbox from starting until the network is up and available.
# SWITCHES:    [-v] -- for verbose, only used during development + testing!
 
# ref: http://wiki.bash-hackers.org/commands/builtin/printf
# ref: http://stackoverflow.com/questions/3224878/what-is-the-purpose-of-the-colon-gnu-bash-builtin
 
progname="dropbox"
wireless="wlan0"
ifcfgCmd="ifconfig $wireless | grep \"inet addr:\" | cut -d\":\" -f2 | awk '{print \$1}'"
dropbCmd="dropbox start -i"
 
# verbose output?
if [ "$1" == "-v" ]; then
  vpf=printf
  vecho=echo
else
  vpf=":"
  vecho=":"
fi
 
check_process() {
  $vpf "%-25s" "$1 running?"
  [ "$1" = "" ] && return 0
  [ `pgrep -nx $1` ] && return 0 || return 1
}
 
check_network() {
  $vpf "%-25s" "device $1 running?"
  [ `eval $ifcfgCmd` ] && return 0 || return 1
}
 
$vecho "begin checking..."
 
# wait for network
while [ 1 ]; do
  # wlan0 up?
  check_network "$wireless"
  if [ $? -eq 0 ]; then
    $vecho "Yup" && break
  else
    $vecho "Nope"
  fi
  sleep 10
done
 
# wait for progname
while [ 1 ]; do
  # already running?
  check_process "$progname"
  if [ $? -eq 0 ]; then
    $vecho "Yup" && break
  else
    $vecho "Nope" && `eval $dropbCmd > /dev/null`
  fi
  sleep 10
done
 
$vecho -e "$progname running w/ PID:  $(pgrep -x "$progname")\n"
exit 0
# vim: set ts=2 :

So now when my laptop starts up the Dropbox entry under System -> Preferences -> Startup Applications now runs my script, /home/saml/bin/start_dropbox.bash instead of dropbox start -i. The script’s behavior looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# NOTE1: NetworkManager is off
# NOTE2: dropbox isn't running
 
# NOTE3: the output below is generated with the -v switch, this is used only for testing purposes!
 
# Shell #1
% start_dropbox.bash -v
begin checking...
device wlan0 running?    Nope
device wlan0 running?    Nope
device wlan0 running?    Nope
device wlan0 running?    Nope
...
 
# Shell #2
% sudo /etc/init.d/NetworkManager start
 
# Shell #1
device wlan0 running?    Nope
device wlan0 running?    Yup
dropbox running?         Nope
dropbox running?         Yup
dropbox running w/ PID:  27112

References

links

NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.