-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cancel current execution after of a new event #93
Comments
@DasHaus I created something very similar for a project I'm working on. Basically, you find the PID of the running script and kill it. You may need to change the # Find the PIDs
pids=$(ps -ef | grep "fswatch" | grep -v grep | awk '{print $2}')
# Kill them
if [ -n "$pids" ]; then
kill -9 $pids
fi
# ... |
And How I can detect when a new event is raised?. Thanks you very much! |
@DasHaus You will just put that at the beginning of your current script. If any fswatch's are running, they will be stopped, and then yours will be started. |
@DasHaus Oh, wait. I see what you mean. It will look something like:
fswatch ts |
while read; do
# Find the PIDs
pids=$(ps -ef | grep -E "babel|tsc" | grep -v grep | awk '{print $2}')
# Kill them
if [ -n "$pids" ]; then
kill -9 $pids
fi
#Start compiling
./compile.bash
done
echo "changes received";
echo "compiling typescript files..";
tsc -p ts/;
echo "compiling files with babel...";
babel ES6 -d scripts --babelrc ES6/babelconfig.json;
echo "OK"; |
thanks, then I have this in this moment: compile(){
p "compiling typescript files.." &&
tsc -p ts/ &&
p "transpiling files with babel..." &&
babel -q ES6 -d scripts --babelrc ts/babelconfig.json &&
p "OK";
p "waiting for new changes...";
}
compile;
fswatch ts |
while read; do
# Find the PIDs
pids=$(ps -ef | grep -E "babel|tsc" | grep -v grep | awk '{print $2}')
# Kill them
if [ -n "$pids" ]; then
kill -9 $pids
fi
p "changes received";
compile;
done And don't works... The problem is that the block inside of |
The new event is executed when the previous event ends. I don't know if is possible read the events queue in fswatch |
That's expected:
or
instead. |
Aaaah, ok!. I understand now. The p(){
now="$(date +'%r')"
printf "$(tput setaf 1)%s$(tput sgr0) | $(tput bold)$1$(tput sgr0)\n" "$now";
}
compile(){
p "compiling typescript files.." &&
tsc -p ts/ &&
p "transpiling files with babel..." &&
babel -q ES6 -d scripts --babelrc ts/babelconfig.json &&
p "OK!" &&
p "waiting for new changes...";
}
compile &
fswatch ts |
while read; do
p "new changes received!"
# Find the PIDs
pids=$(ps -ef | grep -E "babel|tsc" | grep -v grep | awk '{print $2}')
# Kill them
if [ -n "$pids" ]; then
p "cancelling actual event..."; kill -9 $pids &> /dev/null
fi
compile &
done It works like a charm! Thanks you both! 👍 |
You're welcome. I suggested you using a separate script for the compilation job so that it would be easier to isolate its PID and kill it (and its child processes). I'm glad you've found a working solution. |
Hi, long_command(){
clear
printf "Starting"
sleep 1
printf "."
sleep 1
printf "."
sleep 1
printf "."
sleep 1
printf " done!\n"
}
cancel_and_run() {
while read; do
if [ ${command_pid:-0} -gt 0 ]; then
# Loop until process is effectively killed (synchronous)
while pkill -P $command_pid 2>/dev/null; do sleep 0.5 ; done
fi
( long_command ) &
local command_pid=$!
done
}
fswatch somefile | cancel_and_run |
I have the following script:
the execution inside of
while
take 15 seconds, I want that when a new event is raised cancel the current execution and execute it again. How can I do? I know really little about shell script...The text was updated successfully, but these errors were encountered: