Comments (5)
It doesn't have anything like that built-in, but I think you can use these to achieve the same thing:
_ = Timer.init(timeInterval: 1, repeats: false) { _ in ... }
DispatchQueue.global(qos: .default).asyncAfter(deadline: DispatchTime.init(uptimeNanoseconds: 1_000_000_000)) {
...
}
from swiftshell.
So you can first start an async command, and then use 1 of the methods above to cancel it after a certain time.
from swiftshell.
Thanks. I started investigating this script
#!/bin/bash
#
# The Bash shell script executes a command with a time-out.
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
# is blocked, then the subsequent SIGKILL (9) terminates it.
#
# Based on the Bash documentation example.
# Hello Chet,
# please find attached a "little easier" :-) to comprehend
# time-out example. If you find it suitable, feel free to include
# anywhere: the very same logic as in the original examples/scripts, a
# little more transparent implementation to my taste.
#
# Dmitry V Golovashkin <[email protected]>
scriptName="${0##*/}"
declare -i DEFAULT_TIMEOUT=9
declare -i DEFAULT_INTERVAL=1
declare -i DEFAULT_DELAY=1
# Timeout.
declare -i timeout=DEFAULT_TIMEOUT
# Interval between checks if the process is still alive.
declare -i interval=DEFAULT_INTERVAL
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
declare -i delay=DEFAULT_DELAY
function printUsage() {
cat <<EOF
Synopsis
$scriptName [-t timeout] [-i interval] [-d delay] command
Execute a command with a time-out.
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
signal is blocked, then the subsequent SIGKILL (9) terminates it.
-t timeout
Number of seconds to wait for command completion.
Default value: $DEFAULT_TIMEOUT seconds.
-i interval
Interval between checks if the process is still alive.
Positive integer, default value: $DEFAULT_INTERVAL seconds.
-d delay
Delay between posting the SIGTERM signal and destroying the
process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
As of today, Bash does not support floating point arithmetic (sleep does),
therefore all delay/time values must be integers.
EOF
}
# Options.
while getopts ":t:i:d:" option; do
case "$option" in
t) timeout=$OPTARG ;;
i) interval=$OPTARG ;;
d) delay=$OPTARG ;;
*) printUsage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
# $# should be at least 1 (the command to execute), however it may be strictly
# greater than 1 if the command itself has options.
if (($# == 0 || interval <= 0)); then
printUsage
exit 1
fi
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
(
((t = timeout))
while ((t > 0)); do
sleep $interval
kill -0 $$ || exit 0
((t -= interval))
done
# Be nice, post SIGTERM first.
# The 'exit 0' below will be executed if any preceeding command fails.
kill -s SIGTERM $$ && kill -0 $$ || exit 0
sleep $delay
kill -s SIGKILL $$
) 2> /dev/null &
exec "$@"
I got it form this answer here. Saved it into an SH file and now im testing it out. If it fails then wil try your suggestion thanks.
Will post results here.
from swiftshell.
Did you find a solution?
from swiftshell.
sorry. i got tied up with projects.
closing this.
will update here once i get around to it.
from swiftshell.
Related Issues (20)
- runAsync(bash"") cannot write nothing on onStringOutput on mac osx app standalone HOT 2
- `waitUntilExit` cause slow execution HOT 1
- How to execute rm -rf /xxx/* HOT 3
- Could not find module 'SwiftShell' for target 'arm64-apple-macos'; found: x86_64, x86_64-apple-macos
- How to execute an ncurses-based command? HOT 2
- run shell empty output HOT 4
- PID of the command launched using SwiftShell
- The interactive command is suspended at the terminal
- Im trying to find a way to make a run cancellable
- Not support `Pod` command HOT 5
- New syntax? HOT 2
- Xcode 12 beta / Swift 5.3 not building HOT 5
- Tupling issue? HOT 1
- Request for an API that do not exit when executable to run is not found HOT 2
- Final released Swift 5.3 crashes on compile HOT 10
- Using ubuntu-latest for GHAction Test HOT 4
- Make run and throwable HOT 1
- onStringOutput returning nothing.
- Allow `run` to take an array HOT 1
- How to pipe with different contexts?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftshell.