Advertisement
fant0men

runnet_receiver.sh

Nov 1st, 2019 (edited)
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script runs the list of commands and echoes output to a file
  4. # as well as a short log describing the command and exit status.
  5. # List open file descriptors:
  6. # ls -la /proc/$$/fd
  7.  
  8. script_bn=$(basename "$0")
  9. script_fn=$(readlink -f "$0")
  10. script_ppid="$$"
  11. dir1='/home/buddha'
  12. dir2="${dir1}/run"
  13. dir3="${dir2}/old"
  14. bn='run_commands'
  15. switch_f="${dir2}/${bn}_switch.txt"
  16. txt1="${dir2}/${bn}.log"
  17. txt2="${dir2}/${bn}_stdout.log"
  18. txt3="${dir2}/${bn}_list.txt"
  19.  
  20. exit_status=0
  21.  
  22. # If the script isn't run with sudo / root privileges, then ask the user
  23. # to type his / her password, so we can run the script with full root
  24. # privileges. 'exec' is used in conjunction with 'sudo bash', thereby
  25. # replacing the current shell, and current instance of the script, with
  26. # the new one that has full privileges.
  27. if [[ $EUID -ne 0 ]]; then
  28.     exec sudo bash -c "$0"
  29. fi
  30.  
  31. # Clears the screen.
  32. clear
  33.  
  34. # Creates a variable called 'regex', which contains a regex that will
  35. # be used to check if an array element / string is truly empty.
  36. regex='^[[:space:]]*$'
  37.  
  38. # Stops the login manager.
  39. service lightdm stop
  40. killall -9 xiccd
  41.  
  42. # Sets the TTY to blank the screen after 1 minute of inactivity.
  43. # Turns off powersave features of the TTY.
  44. setterm --blank 1 --powersave off --powerdown 0
  45.  
  46. # Creates a function called 'get_tty', which will figure out what TTY
  47. # the script is currently running on. If the current instance of the
  48. # script doesn't know, then get that information from the previous
  49. # instance of the script (the first argument given to the script when
  50. # reloading using 'exec').
  51. get_tty () {
  52.     tty_tmp=$(tty)
  53.  
  54.     if [[ ! $tty_tmp =~ ^/dev/tty[0-9]+ ]]; then
  55.         tty_tmp="$1"
  56.     fi
  57.  
  58.     echo "$tty_tmp"
  59. }
  60.  
  61. c_tty=$(get_tty "$1")
  62.  
  63. # Creates the output directory, as well as the text / log files
  64. # (if they don't already exist).
  65. mkdir -p "$dir3"
  66.  
  67. echo 0 > "$switch_f"
  68.  
  69. touch "$txt1" "$txt2" "$txt3"
  70.  
  71. sync
  72.  
  73. # Changes permission recursively for $dir2, so that my main PC
  74. # will be able to read and write to those files over the network.
  75. chown -R buddha "$dir2"
  76. chmod -R ugo+rw "$dir2"
  77.  
  78. # trap ctrl-c and call ctrl_c()
  79. trap ctrl_c INT
  80.  
  81. ctrl_c () {
  82.     restore
  83.     kill_children
  84.     echo '** Trapped CTRL-C'
  85.     exit
  86. }
  87.  
  88. # Creates a function called 'flush', which will flush the cache
  89. # (free pagecache, dentries and inodes).
  90. flush () {
  91.     while [[ 1 ]]; do
  92.         sleep 30m
  93.         echo 3 > /proc/sys/vm/drop_caches
  94.     done
  95. }
  96.  
  97. flush &
  98.  
  99. # Creates a function called 'restore', which will restore STDOUT and
  100. # STDERR, as well as close file descriptor 3.
  101. restore () {
  102.     sync
  103.     exec &>"$c_tty"
  104.     exec 3>&-
  105. }
  106.  
  107. # Creates a function called 'kill_children', which will kill all child
  108. # processes to this script.
  109. kill_children () {
  110.     mapfile -t child < <(ps -C "$script_bn" -o pid= | sort -rn)
  111.  
  112.     for (( i = 0; i < ${#child[@]}; i++ )); do
  113.         if [[ ! ${child[${i}]} =~ $regex ]]; then
  114.             child_pid=$(tr -d '[:blank:]' <<<"${child[${i}]}")
  115.  
  116.             if [[ $i -ne $script_ppid ]]; then
  117.                 echo "SIGTERM: ${script_bn} (${child_pid})"
  118.                 kill -s 15 "$child_pid"
  119.             fi
  120.         fi
  121.     done
  122. }
  123.  
  124. # Creates the 'switch' function, which will change $switch_f to '1'
  125. # if it's '0', or '0' if it's '1'.
  126. switch () {
  127.     switch=$(cat "$switch_f")
  128.  
  129.     if [[ $switch -eq 0 ]]; then
  130.         echo '1' > "$switch_f"
  131.     elif [[ $switch -eq 1 ]]; then
  132.         echo '0' > "$switch_f"
  133.     fi
  134. }
  135.  
  136. # Creates a function called 'init_log', to initiate the $count variable,
  137. # which will be used to rotate the STDOUT log, once it exceeds
  138. # 10000 lines. The function looks to see if there are any old log files
  139. # and bases $count on the previous last log file.
  140. init_log () {
  141.     count=0
  142.     declare old_log_tmp
  143.  
  144.     mapfile -t old_log < <(ls -1 "$dir3")
  145.  
  146.     if [[ ! ${old_log[0]} =~ $regex ]]; then
  147.         for (( i = 0; i < ${#old_log[@]}; i++ )); do
  148.             if [[ ! ${old_log[${i}]} =~ $regex ]]; then
  149.                 mapfile -d'.' -t line <<<"${old_log[${i}]}"
  150.                 el=$(( ${#line[@]} - 1 ))
  151.                 old_log_tmp+="${line[${el}]}\n"
  152.             fi
  153.         done
  154.  
  155.         count=$(echo -e "$old_log_tmp" | sort -rn | head -n 1)
  156.     fi
  157.  
  158.     echo "$count"
  159. }
  160.  
  161. # Creates a function called 'rotate_log', which will rotate $txt2 if
  162. # it exceeds 10000 lines.
  163. rotate_log () {
  164.     lines=$(cat "$txt2" | wc -l)
  165.  
  166.     if [[ $lines -ge 10000 ]]; then
  167.         let count++
  168.  
  169.         new="${txt2}.${count}"
  170.  
  171. # ls -la /proc/$$/fd
  172.  
  173.         cp -p "$txt2" "$new"
  174.         mv "$new" "$dir3"
  175.  
  176.         truncate --size=0 "$txt2"
  177.     fi
  178.  
  179.     echo "$count"
  180. }
  181.  
  182. # Creates a function called 'pid_running', which waits until the last
  183. # executed command has finished.
  184. pid_running () {
  185.     pid="$!"
  186.  
  187. # is=$(kill -0 $pid 2>&-)
  188.  
  189. # while [[ $is ]]; do
  190. # sleep 1
  191. # pid=$(kill -0 $pid 2>&-)
  192. # done
  193.  
  194.     wait $pid
  195.  
  196.     exit_status="$?"
  197. }
  198.  
  199. # Creates a function called 'reload', which reloads this script when
  200. # called.
  201. reload () {
  202.     echo '** Reloading script...'
  203.  
  204.     write_list
  205.  
  206.     mapfile -t s_pid < <(pgrep "$script_bn")
  207.  
  208.     echo "tty: $c_tty"
  209.     echo "script: $script_fn"
  210.     echo "pid: ${s_pid[@]}"
  211.  
  212.     restore
  213.     kill_children
  214. # switch
  215.     exec "$script_fn" "$c_tty"
  216. }
  217.  
  218. # Creates a function called 'run_cmd', which will run all commands in
  219. # $txt3.
  220. run_cmd () {
  221.     mapfile -t functions < <(declare -F | grep -v '^declare -fx' | sed 's/^declare -f //')
  222.  
  223.     case ${run[0]} in
  224.         'cd')
  225.             eval "${run[@]}"
  226.         ;;
  227.         *)
  228.             for func in "{functions[@]}"; do
  229.                 if [[ ${run[0]} == "$func" ]]; then
  230.                     eval "${run[@]}"
  231.                     return
  232.                 fi
  233.             done
  234.  
  235.             eval "${run[@]}" &
  236.  
  237.             pid_running
  238.         ;;
  239.     esac
  240. }
  241.  
  242. # Creates a function called 'rw_log', which opens file descriptors to
  243. # append to the log files.
  244. # Specifically:
  245. # * Redirect both STDOUT and STDERR to $txt2
  246. # * Opens file descriptor 3 as output to $txt1
  247. rw_log () {
  248.     exec &>>"$txt2"
  249.     exec 3>>"$txt1"
  250. }
  251.  
  252. # Creates a function called 'clear_log', which clears the log files
  253. # if called.
  254. clear_log () {
  255.     truncate --size=0 "$txt2" "$txt1"
  256. }
  257.  
  258. # Creates a function called 'upgrade', which will do a complete system
  259. # upgrade (install all available updates).
  260. upgrade () {
  261.     apt-get update &
  262.     pid_running
  263.     apt-get --yes dist-upgrade &
  264.     pid_running
  265.     apt-get --yes autoclean &
  266.     pid_running
  267.     apt-get --yes autoremove &
  268.     pid_running
  269. }
  270.  
  271. irssi () {
  272.     openvt -c 9 -f -- runuser buddha -c 'irssi -c EFNet'
  273. }
  274.  
  275. # Creates a function called 'write_list', which overwrites the command
  276. # list, discarding the first line.
  277. write_list () {
  278.     truncate --size=0 "$txt3"
  279.  
  280.     for (( i = 1; i < ${#list[@]}; i++ )); do
  281.         echo -n "${list[${i}]}" >> "$txt3"
  282.     done
  283.  
  284.     sync
  285. }
  286.  
  287. rw_log
  288. count=$(init_log)
  289.  
  290. while [[ 1 ]]; do
  291.     mapfile -t list < <(cat "$txt3" 2>&-)
  292.  
  293.     mapfile -d' ' -t run <<<"${list[0]}"
  294.  
  295.     if [[ ${run[0]} =~ $regex ]]; then
  296.         continue
  297.     fi
  298.  
  299. # Sender script needs to wait...
  300.     switch
  301.  
  302.     count=$(rotate_log)
  303.  
  304.     date=$(date "+%F %H:%M:%S")
  305.  
  306.     echo
  307.  
  308.     run_cmd
  309.  
  310.     echo >&3
  311.     echo "$date" >&3
  312.     echo 'COMMAND:' >&3
  313.     echo "${run[@]}" >&3
  314.     echo "EXIT STATUS: $exit_status" >&3
  315.  
  316.     write_list
  317.  
  318. # Now sender script can continue...
  319.     switch
  320. done
  321.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement