Guest User

Untitled

a guest
May 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # restarts program upon file changing, useful for writing code without having to restart it.
  4. # also accepts a enter key to restart the program
  5. # bugs, only works on scripts, kills everything with the name of the script except this script itself.
  6.  
  7. FULLCOMMAND="$@"
  8. COMMAND=${FULLCOMMAND%% *}
  9.  
  10. # check if args set
  11. if [ -z $1 ]; then
  12. echo "Usage $0 /full/path/to/myScript.xyz <arguments>"
  13. exit 1
  14. fi
  15. FILENAME=$(basename $COMMAND)
  16.  
  17. # Binary checking
  18. #for i in inotifywait basename ps grep ;do
  19. for i in basename ps grep ;do
  20. command -v $i >/dev/null 2>&1 || { echo >&2 "$0 - I require $i but it's not installed. Aborting."; exit 1; }
  21. done
  22.  
  23.  
  24. # check if file actually exists
  25. if [ ! -f "$COMMAND" ]; then
  26. echo "File $COMMAND not found!"
  27. exit 1
  28. fi
  29.  
  30. function MonitorForChange() {
  31. # either waits for a file modification date change or a keypress
  32. origFilestamp=$(ls --time-style='+%d-%m-%Y %H:%M:%S' -l ${COMMAND})
  33. lastFilestamp=""
  34. while true; do
  35. lastFilestamp=$(ls --time-style='+%d-%m-%Y %H:%M:%S' -l ${COMMAND})
  36. if [ "$lastFilestamp" != "$origFilestamp" ] ; then
  37. return
  38. fi
  39. read -t 0.1 -n 1 foo
  40. if [ $? == 0 ]; then
  41. return
  42. fi
  43. done
  44. }
  45.  
  46. # kill it
  47. function Kill() {
  48. # dont kill this script but everything with the FILENAME var
  49. $(ps -ef | grep $FILENAME | awk '{print $2}' | grep -v ^$$\$ | xargs kill -15)
  50. # kill $PID 2>/dev/null | xargs
  51. }
  52.  
  53. # start it
  54. function Start() {
  55. # tweak to suit
  56. # $($FULLCOMMAND)
  57. source ${FULLCOMMAND} &
  58. # python ${FULLCOMMAND} &
  59. # bash -c "FULLCOMMAND"
  60. }
  61.  
  62. while true; do
  63. Kill
  64. Start
  65. MonitorForChange
  66. echo "$0 - Program changed!"
  67. done
Add Comment
Please, Sign In to add comment