Uunter Linux habe ich nach einem Script gesucht mit welchem ich Dropbox sowohl beim Booten als auch über die Kommandozeie steuern kann. Dazu habe ich als Basis das Python Script von Dropbox verwendet. Darum herum habe ich den folgenden Bash-Wrapper geschrieben. Das Script kennt den -u oder –user Parameter mit welchem man Dropbox auch beim Systemstart laden kann. Das läuft dann unter root und wechselt mittels su zum entsprechenden User für den Aufruf des Python Scripts. Der Wrapper lässt nicht zu, dass das Python Script unter root ausgeführt wird!
Für den Systemstart legt man sich dann ein Startscript an, welches den Wrapper aufruft
#!/bin/bash CONF=/etc/sysconfig/dropbox BIN=`which dropbox-ctl` [[ ! -e $CONF || ! -f $CONF ]] && echo 'config file not found' && exit 1 [[ ! -e $BIN || ! -f $BIN || ! -x $BIN ]] && echo 'dropbox-ctl not found' && exit 1 source /etc/sysconfig/dropbox for usr in $DROPBOX_USERS ; do $BIN -u $usr start done
In /etc/sysconfig/dropbox kann man die User definieren für die Dropbox gestartet werden soll
1 | DROPBOX_USERS="user1 user2 user3 userX" |
Zustätzlich kann man in dem Konfigfile auch die default Wert für lansync und autostat vom Wrapper Script angeben
1 2 | DROPBOX_LANSYNC=n DROPBOX_AUTO=n |
#!/bin/bash ########################################################################################### # Litte bash wrapper script for managing dropbox on linux # Download the necessary python script here # https://www.dropbox.com/download?dl=packages/dropbox.py # # this script takes several parameters # start|stop|restart selfspeaking :-) # status shows if dropbox is running # running print 1 if dropbox is running for USER, 0 otherwise # lansync y|n enables/disables lansync default disabled # autostart y|n enables/disables autostart default disabled # -q|--quiet no output at all. Only exit codes # -u|--user specifies user to run the python script # only necessary if root callls the wrapper script # if non-root user is calling the wrapper the script # this parameter will be disregarded # -s|--script defines the path to the python script # if omitted the script tries to find it first in the directory # where itself resides followed by the actual working dir # ########################################################################################### PWD=`dirname $BASH_SOURCE` SCRIPT_NAME='dropbox.py' CONFIG=/etc/sysconfig/dropbox CONF='' QUIET='' while [[ $# > 0 ]] ; do case "$1" in start|stop|restart) ACTION="$1" ;; status|running) ACTION="$1" ;; lansync|autostart) CONF="$CONF$1" shift [ "x$(echo $1|grep -i ^[ny]$)" = 'x' ] && echo 'only case-insensitiv Y or N allowed' && exit 1 CONF="$CONF $1 " ;; -u|--user) shift USER="$(echo $1|grep ^[a-zA-Z_\-.]*$)" ;; -q|--quiet) QUIET='y' ;; -s|--script) shift SCRIPT="$(echo $1|grep ^[a-zA-Z0-9_\-/.]*$)" ;; -h|--help) echo echo '###########################################################################################' echo '# Litte bash wrapper script for managing dropbox on linux' echo '# Download the necessary python script here' echo '# https://www.dropbox.com/download?dl=packages/dropbox.py' echo '#' echo '# this script takes several parameters' echo '# start|stop|restart selfspeaking :-)' echo '# status shows if dropbox is running' echo '# lansync y|n enables/disables lansync default disabled' echo '# autostart y|n enables/disables autostart default disabled' echo '# -q|--quiet no output at all. Only exit codes' echo '# -u|--user specifies user to run the python script' echo '# only necessary if root callls the wrapper script' echo '# if non-root user is calling the wrapper the script' echo '# this parameter will be disregarded' echo '# -s|--script defines the path to the python script' echo '# if omitted the script tries to find it first in the directory' echo '# where itself resides followed by the actual working dir' echo '#' echo '###########################################################################################' echo exit 0 ;; *) echo "Usage $0 start|stop|restart|lansync|autostart -q|--quiet -u|--user -s|--script" exit 0 ;; esac shift done function do_main() { # if non-root user overwrite USER [ "$(whoami)" != 'root' ] && USER=`whoami` # exit if USER not set [ "x$USER" = 'x' ] && echo 'ERROR: User not given. See -u|--user if running as root' && return 1 # exit if ACTION not set [ "x$ACTION" = 'x' ] && echo 'ERROR: Action must be given start|stop|restart|status|running' && return 1 # check if USER exists id -u "$USER" >/dev/null 2>&1 # exit if USER not exists [ $? -ne 0 ] && echo "ERROR: User $USER does not exist" && return 1 return 0 } function chk_config() { # check if python script is found in the wrappers directory # set if found [[ "x$SCRIPT" = 'x' && -f ${PWD}/${SCRIPT_NAME} && -x ${PWD}/${SCRIPT_NAME} ]] && SCRIPT="${PWD}/${SCRIPT_NAME}" # check if python script is found in the actual working dir # set if found [[ "x$SCRIPT" = 'x' && -f ./${SCRIPT_NAME} && -x ./${SCRIPT_NAME} ]] && SCRIPT="./${SCRIPT_NAME}" # exit if SCRIPT is not set [ "x$SCRIPT" = 'x' ] && echo 'ERROR: Script must be specified' && return 1 # exit if SCRIPT not found or not executable [[ ! -f $SCRIPT || ! -x $SCRIPT ]] && echo "ERROR: Script $SCRIPT not found AND/OR not executable" && return 1 # source the config source $CONFIG # set default values for lansync and autostart parameter [ "x$(echo $CONF | grep lansync)" = 'x' ] && CONF="lansync $DROPBOX_LANSYNC" [ "x$(echo $CONF | grep autostart)" = 'x' ] && CONF="${CONF} autostart $DROPBOX_AUTO" CONF_AUTO=$(echo $CONF | awk -F'autostart ' '{print $2}' | awk '{print $1}') CONF_SYNC=$(echo $CONF | awk -F'lansync ' '{print $2}' | awk '{print $1}') } function do_action() { # everything but restsrt if [ "$(whoami)" = 'root' ] ; then # exit if running as root AND -u|--user root specified [ "$USER" = 'root' ] && echo 'ERROR: Can only run as root if -u|--user is supplied' && return 1 # as root use su to switch to -u|--user su ${USER} -c "${SCRIPT} ${ACTION}" else # run if non-root ${SCRIPT} ${ACTION} fi } function do_config(){ if [ "$(whoami)" = 'root' ] ; then [ "$ACTION" = 'start' ] && sleep 1 && su ${USER} -c "${SCRIPT} autostart ${CONF_AUTO}" [ "$ACTION" = 'start' ] && su ${USER} -c "${SCRIPT} lansync ${CONF_SYNC}" else [ "$ACTION" = 'start' ] && sleep 1 && ${SCRIPT} autostart ${CONF_AUTO} [ "$ACTION" = 'start' ] && ${SCRIPT} lansync ${CONF_SYNC} fi } if [ "x$QUIET" = 'x' ] ; then do_main || exit 1 chk_config || exit 1 else do_main >/dev/null 2>&1 || exit 1 chk_config >/dev/null 2>&1 || exit 1 fi if [ "$ACTION" = 'restart' ] ; then ACTION='stop' if [ "x$QUIET" = 'x' ] ; then do_action || exit 1 sleep 1 ACTION='start' do_action || exit 1 sleep 1 do_config else do_action >/dev/null 2>&1 || exit 1 sleep 1 ACTION='start' do_action >/dev/null 2>&1 || exit 1 sleep 1 do_config fi elif [[ "$ACTION" = 'status' || "$ACTION" = 'running' ]] ; then if [ "x$QUIET" = 'x' ] ; then do_action || exit 1 else do_action >/dev/null 2>&1 || exit 1 fi else if [ "x$QUIET" = 'x' ] ; then do_action sleep 1 do_config else do_action >/dev/null 2>&1 sleep 1 do_config >/dev/null 2>&1 fi fi
Leave a Reply