Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # restarts program upon file changing, useful for writing code without having to restart it.
- # also accepts a enter key to restart the program
- # bugs, only works on scripts, kills everything with the name of the script except this script itself.
- FULLCOMMAND="$@"
- COMMAND=${FULLCOMMAND%% *}
- # check if args set
- if [ -z $1 ]; then
- echo "Usage $0 /full/path/to/myScript.xyz <arguments>"
- exit 1
- fi
- FILENAME=$(basename $COMMAND)
- # Binary checking
- #for i in inotifywait basename ps grep ;do
- for i in basename ps grep ;do
- command -v $i >/dev/null 2>&1 || { echo >&2 "$0 - I require $i but it's not installed. Aborting."; exit 1; }
- done
- # check if file actually exists
- if [ ! -f "$COMMAND" ]; then
- echo "File $COMMAND not found!"
- exit 1
- fi
- function MonitorForChange() {
- # either waits for a file modification date change or a keypress
- origFilestamp=$(ls --time-style='+%d-%m-%Y %H:%M:%S' -l ${COMMAND})
- lastFilestamp=""
- while true; do
- lastFilestamp=$(ls --time-style='+%d-%m-%Y %H:%M:%S' -l ${COMMAND})
- if [ "$lastFilestamp" != "$origFilestamp" ] ; then
- return
- fi
- read -t 0.1 -n 1 foo
- if [ $? == 0 ]; then
- return
- fi
- done
- }
- # kill it
- function Kill() {
- # dont kill this script but everything with the FILENAME var
- $(ps -ef | grep $FILENAME | awk '{print $2}' | grep -v ^$$\$ | xargs kill -15)
- # kill $PID 2>/dev/null | xargs
- }
- # start it
- function Start() {
- # tweak to suit
- # $($FULLCOMMAND)
- source ${FULLCOMMAND} &
- # python ${FULLCOMMAND} &
- # bash -c "FULLCOMMAND"
- }
- while true; do
- Kill
- Start
- MonitorForChange
- echo "$0 - Program changed!"
- done
Add Comment
Please, Sign In to add comment