kordwarshuis / cardgame

0 stars 4 forks source link

Adjust Cronjob Digistate for create-tweets-history.php #124

Closed henkvancann closed 3 years ago

henkvancann commented 3 years ago

Henk has to do this. Kor assign Henk pls

kordwarshuis commented 3 years ago

Please add create-tweets-history.php to /home/blockcbird/public_html/t/twitter-phirehose/script_phirehose.sh

henkvancann commented 3 years ago

Because create-tweets-history.php is a not permanently running script, I think I don't to add it to the kill command.

This is the new script script_phirehose.sh

#!/bin/bash
#Cronjob to stop and restart Phirehose twitter feeds
# hvancann@bcws.io
cd ~/public_html/t/twitter-phirehose/
#Be very aware that these variables are crucial for the survival of your running system!!
NAMESTR="phirehose"
UIDSTR="blockcb"
MIN_LEN1=8
MIN_LEN2=5
KILL_GO=0
errcode=0
# <- Be very sure to use deterministic string to select from the list of PIDs

#Check the length of the input strings. If they are long enough the kill comaand will proceed!
#Again: this is a loose canon.
if [[ ${#NAMESTR} > ${MIN_LEN1} ]] && [[ ${#UIDSTR} > ${MIN_LEN2} ]]; 
then KILL_GO=1
fi

#Find the processes, select only those that have both NAMESTR and UIDSTR 
#and isolate the list of PIDs
if [[ ${KILL_GO} ]];
then {
  echo "Every process that contains '${NAMESTR}' in name and '${UIDSTR}' in UID will be killed.";
  errcode=$(ps -ef | grep $NAMESTR | grep $UIDSTR | grep -v $$ | tr -s ' ' | cut -d ' ' -f2 | xargs kill -s 9 2> /dev/null ); 
  # sttderr -> null: we don't want to know that self-kill did not succeed.
  # grep -v $$ -> exclude the PID of this bash script
  echo "Error code of kill pipe: " $errcode >&2
  #Example   ps -ef | grep phirehose | grep blockcb | tr -s ' ' | cut -d ' ' -f2 ...
  # creates tweets history https://github.com/kordwarshuis/cardgame/issues/124 
  ./create-tweets-history.php

  # start the service again:
  ./start_phirehose.sh; 
  exit 0 #the script succeeded
}
else {
  echo "No kill op processes by $$"
  echo "Length of argument '$NAMESTR' is ${#NAMESTR}, minimum is $MIN_LEN1.";
  echo "Length of argument '$UIDSTR' is ${#UIDSTR}, minimum is $MIN_LEN2.";
  exit 1
}
fi
henkvancann commented 3 years ago

If it runs ok, you can close the issue @kordwarshuis

henkvancann commented 3 years ago

uptime-watch-restart.sh

#!/bin/bash
# Read Uptime server and check that it hasn't gone down recently, and if then restart scripts
#################################
#   PRECONDITION 'Uptime -p' delivers space divided result in this format:
#   'up 6 days, 10 hours, 3 minutes'
# If the server did go down then (t < 1 minute): restart script-phirephose.sh
## script-phirehose kills process by itself
# else
# exit with good code
NUMDAYS=$(uptime -p | cut -d ' ' -f2)
NUMHRS=$(uptime -p | cut -d ' ' -f4)
NUMMINS=$(uptime -p | cut -d ' ' -f6)
THRESHOLD=5  # minutes

if [[ ${#NUMDAYS} == 0 ]] && [[ ${#NUMHRS} == 0 ]] &&  [[ ${#NUMMINS} < ${#THRESHOLD}]]; 
then {
   # We have a situation where the server has just restarted and is up
   # So we want the following scripts to restart even if they have already
    script-phirephose.sh
   #    script-phirephose also kills the current watcher script
  }
fi
# else the server is up longer than THRESHOLD minutes and we won't have to do anything
henkvancann commented 3 years ago

uptime-watch-restart.sh is not used for now because we found the option for a cornjob in Direct Admin: "run after @reboot" and selected this option ![Uploading Screenshot 2021-02-17 at 12.34.58.png…]()

kordwarshuis commented 3 years ago

There were issues, but they are finally fixed. One of the issues was moving from .com to .org