Crontab + Seeing App System Tray (KDE)

I’ve written a script that loads multiple instances of Dropbox (below). It works fine when I run it from the command line.

I noticed that when I leave the computer and it locks the screen so I have to log on again that the dropbox instances also drop from memory so I have to run the script again when I log in.

So, to save having to remember that I put the script on a personal (not system) cronjob. This works in the sense that I can see that it keeps the instances in memory. However, once the cronjob loads this into memory the instances no longer show in system tray. I guess this could be because it’s loading the instances while on the log on screen which means there is no system tray showing, I guess.

In any case is there a simple solution… maybe there’s some sort of check you can do to determine that someone is logged in before continuing through a script? Or maybe I just need to add an env variable to the script?

Anyway, if someone has a solution I would appreciate it.

#!/bin/sh

HOME_DIR=$HOME
DROPBOXES=("$HOME/.dropboxes/personal" "$HOME/.dropboxes/work")
function start_dropbox() {
  HOME=$HOME_DIR
  local flag
  local home_dir
  local silent=0
  local OPTIND;
  local verbose=0
  local wait=0

  while getopts p:svw opt; do
    case $opt in
      p) home_dir="$(echo $OPTARG | sed 's:/*$::')/" ;;
      s) silent=1 ;;
      v) verbose=1 ;;
      w) wait=1 ;;
      *) ;;                                                                                    
    esac                                                                                       
  done                                                                                         
  shift $((OPTIND-1))                                                                          
                                                                                               
  # Test if the process is already running                                                     
  local pid=$(ps aux|grep "${home_dir}.dropbox-dist"|grep -v 'grep'|tr -s ' '| cut -d' ' -f 2) 
  if  -n "$pid" ]                                                                             
  then                                                                                         
    if  $silent -eq 0 ]; then                                                                 
      echo "Process already running with home dir. of: $home_dir"                              
    fi                                                                                         
    return 8 # Process already running                                                         
  fi                                                                                           
                                                                                               
  # Create home directory if it doesn't exist                                                  
  if  ! -e "$home_dir" ]                                                                      
  then                                                                                         
    if mkdir -p "$home_dir";                                                                   
    then                                                                                       
      if  $silent -eq 0 ]; then                                                               
        echo "Created directory: $home_dir"                                                    
      fi                                                                                       
    else                                                                                       
      if  $silent -eq 0 ]; then                                                               
        echo "Failed to create directory: $home_dir"                                           
      fi                                                                                       
      return 9 # Failed                                                                        
    fi                                                                                         
  fi                                                                                           
                                                                                               
  # Set up so works with GUI from command line                                                 
  xauthority="${home_dir}.Xauthority"                                                          
  if  ! -e "$xauthority" ]                                                                    
  then                                                                                         
    ln -s "$HOME/.Xauthority" "$xauthority"                                                    
  fi                                                                                           
                                                                                               
  HOME="$home_dir"                                                                             
                                                                                               
  # Start the dropbox daemon                                                                   
  if  $verbose -gt 0 ]]; then                                                                
    if  $silent -eq 0 ]; then                                                                 
      echo '~/.dropbox-dist/dropboxd & '$home_dir                                              
    fi                                                                                         
  fi                                                                                           
  ~/.dropbox-dist/dropboxd &                                                                   
  if  $wait -eq 0 ]]; then                                                                   
    sleep 2 # Give each instance time to startup completely before starting another one        
  else                                                                                         
    if  $silent -eq 0 ]; then                                                                 
      read -n 1 -s -p 'Press any key to continue.'                                             
      echo                                                                                     
    fi                                                                                         
  fi                                                                                           
}                                                                                              
                                                                                               
function start_dropboxes() {                                                                   
  local dropbox                                                                                
                                                                                               
  for dropbox in "${DROPBOXES@]}"                                                             
  do                                                                                           
    start_dropbox $@ -p "$dropbox"                                                             
  done                                                                                         
}                                                                                              
                                                                                               
#                                                                                              
# For testing & setup we can choose just one to startup                                        
#                                                                                              
while getopts f:swv opt; do                                                                    
  case $opt in                                                                                 
    f) start_dropbox -p "${DROPBOXES$OPTARG]}" # NOTE: bash array indexes start at 0.         
       exit ;;                                                                                 
    *) ;;                                                                                      
  esac                                                                                         
done                                                                                           
OPTIND=1                                                                                       
                                                                                               
start_dropboxes $@                                                                             

I am understanding only half (or less) about what you are doing because of the lack of knowledge about " dropbox". But one thing is for sure, a cron job runs in the background and it has no relation whatsoever with any login session (GUI or not) that may happen to be running at the same moment (or not). So I assume you are on the wrong path to a solution.

Again, not quite understanding what the whole is about, the following looks to me crucial:

I noticed that when I leave the computer and it locks the screen so I have to log on again that the dropbox instances also drop from memory so I have to run the script again when I log in.

To begin with this looks a bit strange to me. When the screen of a GUI session (which DE are you using btw?) is locked, you are NOT loged out. Thus why do you say you have to log on again? Either you are loged out and thus have to log in again, or the screen is locked and you have to unlock it.

I assume that in the first case it is logical you have to do things again, while in the latter case we have to try to understand why things are undone by screen locking.

I’ve been playing around and I found one solution but I don’t know if it’s a good solution. For one thing, it only works on KDE so I’m sure there’s a better way but here are the lines I added:


if  $(ps --User `whoami`|grep kscreenlocker|wc -l) -gt 0 ]]; then
  if  $VERBOSE -gt 0 ]]; then
    echo 'Exiting because screen is locked.'
  fi
  exit;
fi

You can test this with:


loginctl lock-session <session#>
<run script> which should echo 'Exiting because screen is locked.'
loginctl unlock-session <session#>
which should echo a status
<run script>

The full script as follows (cleaned up a little as well):


#!/bin/sh

HOME_DIR=$HOME
DROPBOXES=("$HOME/.dropboxes/personal" "$HOME/.dropboxes/work")
VERBOSE=1
function start_dropbox() {
  HOME=$HOME_DIR
  local flag
  local home_dir
  local OPTIND
  local wait=0

  while getopts p:v:w opt; do
    case $opt in
      p) home_dir="$(echo $OPTARG | sed 's:/*$::')/" ;;
      w) wait=1    ;; # For testing
      *) ;;
    esac
  done
  shift $((OPTIND-1))

  # Test if the process is already running
  local pid=$(ps aux|grep "${home_dir}.dropbox-dist"|grep -v 'grep'|tr -s ' '| cut -d' ' -f 2)
  if  -n "$pid" ]
  then
    if  $VERBOSE -gt 0 ]]; then
      echo "Process already running with home dir. of: $home_dir"
    fi
    return 8 # Process already running
  fi

  # Create home directory if it doesn't exist
  if  ! -e "$home_dir" ]
  then
    if mkdir -p "$home_dir";
    then
      if  $VERBOSE -gt 0 ]]; then
        echo "Created directory: $home_dir"
      fi
    else
      if  $VERBOSE -gt 0 ]]; then
        echo "Failed to create directory: $home_dir"
      fi
      return 9 # Failed
    fi
  fi

  # Set up so works with GUI from command line
  xauthority="${home_dir}.Xauthority"
  if  ! -e "$xauthority" ]
  then
    ln -s "$HOME/.Xauthority" "$xauthority"
  fi

  HOME="$home_dir"

  # Start the dropbox daemon
  if  $VERBOSE -gt 0 ]]; then
    echo '~/.dropbox-dist/dropboxd & '$home_dir
  fi
  ~/.dropbox-dist/dropboxd &
  if  $wait -eq 0 ]]; then
    sleep 2 # Give each instance time to startup completely before starting another one
  else
    if  $VERBOSE -gt 0 ]]; then
      read -n 1 -s -p 'Press any key to continue.'
      echo
    fi
  fi
}

function start_dropboxes() {
  local dropbox

  for dropbox in "${DROPBOXES@]}"
  do
    start_dropbox $@ -p "$dropbox"
  done
}

#
# For testing & setup we can choose just one to startup with the f option
#
while getopts f:wv: opt; do
  case $opt in
    f) start_dropbox -p "${DROPBOXES$OPTARG]}" # NOTE: bash array indexes start at 0.
       exit ;;
    v) VERBOSE=$OPTARG ;;
    *) ;;
  esac
done
OPTIND=1

if  $(ps --User `whoami`|grep kscreenlocker|wc -l) -gt 0 ]]; then
  if  $VERBOSE -gt 0 ]]; then
    echo 'Exiting because screen is locked.'
  fi
  exit;
fi

start_dropboxes $@

A very minor detail, but

grep kscreenlocker|wc -l) -gt 0

looks a bit overdone.

grep -q kscreenlocker

exits with 0 status on the first match found and 1 if not found. And that is all you want to know.

I do not know if you are interested in this side path, but the same statement can be done shorter and more easy to understand:

if ps --User $(whoami) | grep -q kscreenlocker ; then
    echo 'Screenlocker runs'
fi

Always happy to see a cleaner way.

I’m assuming that kscreenlocker is from KDE. Does anyone know if there is something more general that will work no matter what GUI is being used?

I still would like to see an explanation why your application “vanishes” when the screen is locked. I still think that is the point were the problem should be tackled. And not by trying to program around it.

But that is my idea, not knowing exactly what the application is doing.

You and me both but Dropbox is proprietary and I don’t really know how to troubleshoot it, why it would unload itself at all just because the screen is locked I don’t know. Maybe it’s not, maybe it’s just dying and then gets the cron reloads it but there’s some dead stuff somewhere causing a problem. But even if I could determine that was the case because it’s proprietary I don’t think I could do much about it.