rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

griveをgoogleドライブに同期するシェルスクリプト

2020 Apr. 15.


同期スクリプト grive4cron.sh

#!/bin/sh
#
# 2020 Apr. 13.
# 2014 Aug. 03.
# Ryuichi Hashimoto.

# syncronize google-drive
#   check LAN.
#   check grive running.
#   run grive if not running.


# log file
STDOUTLOG="${HOME}/var/log/grive-stdout.log"
STDERRLOG="${HOME}/var/log/grive-stderr.log"
DELETELOG="${HOME}/var/log/grive-delete.log"
SCRIPTLOG="${HOME}/var/log/grive-rerun.log"

# Set IP-address to which test-ICMP-packet is sent.
DEST_ADDRESS='www.yahoo.co.jp'

# Set grive dir in local host
# GRIVEDIR="${HOME}/YOUR-GRIVE-DIR"
GRIVEDIR="${HOME}/grive-my"

##########################
# function               #
#   check LAN connection #
##########################
check_lan() {
  mes_stdout=`\ping -c 1 $DEST_ADDRESS`
  \echo $mes_stdout | \grep 'received, 0% packet loss' | \grep -q -v 'grep'
  if [ $? -eq 0 ]; then
	  \echo 'LAN connected.' >> ${SCRIPTLOG}
	  \return 1
  else
	  \echo 'LAN not connected.' >> ${SCRIPTLOG}
	  \return 2
  fi
}


##################
## main routine ##
## main routine ##
##################
\echo >> ${SCRIPTLOG}
LANG=C \date >> ${SCRIPTLOG}

# if other same processes running, quit.
cmd=`\basename $0`
numcmd=`\ps ax | \grep $cmd | \grep -v "\-c ${cmd}" | \grep -v -c 'grep'`
numcmd=`\expr $numcmd - 1`
if [ ${numcmd} -gt 1 ]; then
  \echo "Another ${cmd} is running." >> ${SCRIPTLOG}
  \exit 1
fi

# check and connect internet
check_lan_result=0
cnt_try_connect=0
until [ $check_lan_result -eq 1 ]
do
  if [ ${cnt_try_connect} -gt 3 ]; then
    \echo 'No internet connection. Abort.' >> ${SCRIPTLOG}
    \exit 1
  fi
  check_lan
  check_lan_result=$?

  if [ ${check_lan_result} -ne 1 ]; then
    cnt_try_connect=`\expr $cnt_try_connect + 1`
  fi
done

# check/run grive.
\ps ax | \grep 'grive\( \|$\)' | \grep -q -v 'grep'
if [ $? -ne 0 ]; then
  \echo 'Starting grive.' >> ${SCRIPTLOG}
  \cd ${GRIVEDIR}

  # save existing logs
  if [ -f ${STDERRLOG} ]; then
    \echo >> ${DELETELOG}
    LANG=C \date >> ${DELETELOG}
    \grep 'delet' ${STDERRLOG} >> ${DELETELOG}
    # mv ${STDERRLOG} ${STDERRLOG}.bak
  fi

  if [ -f ${STDOUTLOG} ]; then
    \mv ${STDOUTLOG} ${STDOUTLOG}.bak
  fi

  # run grive and output new logs
  LANG=C \date > ${STDOUTLOG}
  LANG=C \date >> ${STDERRLOG}
  \grive 1>> ${STDOUTLOG} 2>> ${STDERRLOG}

else
  \echo 'Another Grive already running.' >> ${SCRIPTLOG}
fi

\exit 0


古い同期プロセスが残っていればそれを停止するスクリプト kill-grive4cron.sh

#!/bin/bash
# 2018 Nov. 11.
# 2016 Sep. 19.
# Ryuichi Hashimoto.

# This program kills the jobs  relevent to "grive" which keep running for too long time.


# set variables
griveScript='[ \/]grive4cron.sh( +.*$|$)'
griveCommand='[ \/]grive( +.*$|$)'
agoHours='3'

# Quit program, if now-time is between 0:00 and ${agoHours}hour.
#   because grive would be working right.
nowHour=`\date +%-H`
griveTime=`\ps ax -f | \grep -E "${griveScript}" | \grep -v 'grep' | \awk '{print $5}' | \awk 'NR==1' | \grep ':'`
  # Get time of process-started.
timeResult="$?"
  # result value
  #     0: the process started today
  #     Not 0: the process started another day.

if [ "$timeResult" -ne 0 ]; then
# in the case of another day started of process
  if [ "$nowHour" -lt "$agoHours" ]; then
    \echo 'Grive may run now.' >&2
    \echo 'Quit.' >&2
    \exit 0
  fi
else
# in the case of process started today
  griveHour=$( \echo "$griveTime" | \sed 's/\(^[0-9]\+\)\:.*/\1/' )
  if [ $(( $nowHour - $griveHour )) -lt "$agoHour" ]; then
    \echo 'Grive may run now.' >&2
    \echo 'Quit.' >&2
  fi
fi

# search running grive.
\ps ax -f | \grep -E "${griveScript}" | \grep -vq 'grep'
runGrive="$?"
if [ "$runGrive" -ne 0 ]; then
  \echo 'Grive is not running.' >&2
  \exit 0
fi

# get process numbers of grive-scripts
procNums=($(\ps ax -f | \grep -E "${griveScript}" | \grep -v 'grep' | \awk '{print $2}'))

\echo "process numbers of grive4cron.sh : "${procNums[*]}" " >&2

# get process number of grive-command
for procNum in "${procNums[@]}"
do

  \echo "checking grive4cron-process "$procNum" " >&2

  # get substancial grive process
  \unset griveComNum
  griveComNum=`\ps ax -f | \grep "$procNum" | \grep -E "${griveCommand}" | \grep -v 'grep' | \awk '$3=='"$procNum"'{print $2}'`
  if [ ! -z "${griveComNum:+x}" ]; then
    \break
  fi
done

\echo "process number of grive command : "$griveComNum" " >&2

# in the case of another day started of process, kill grive
if [ "$timeResult" -ne 0 ]; then
  # kill processes
  \kill "${griveComNum}"
  \echo "killed processes : "${griveComNum}"" >&2
  for procNum in "${procNums[@]}"
  do
    \kill "${procNum}"
    \echo "killed processes : "${procNum}"" >&2
  done
else
  # in the case of process started today
  diffTime=$(\expr `date +%s` - `date -d "$griveTime" +%s` )
    # `date +%s` : now time value

  # kill grive-processes which started more than $agoHours hours ago.
  if [ $(( ${diffTime} / 3600 )) -gt ${agoHours} ]; then
    # kill processes
    \kill "${griveComNum}"
    \echo "killed processes : "${griveComNum}"" >&2
    for procNum in "${procNums[@]}"
    do
      kill "${procNum}"
      \echo "killed processes : "${procNum}"" >&2
    done
  fi
fi

exit 0