Skip to content

Commit

Permalink
fix: Define custom timeout function if command not available
Browse files Browse the repository at this point in the history
  • Loading branch information
itislu committed May 17, 2024
1 parent 0eab313 commit 9ccc162
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 42free.sh
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,42 @@ if ! command -v realpath &>/dev/null; then
}
fi

# If timeout command is not available, define a custom function as a replacement
if ! command -v timeout &>/dev/null; then
timeout() {
local duration=$1
local cmd=("${@:2}")

python3 -c "
import subprocess
import sys
def convert_to_seconds(duration):
units = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
if duration[-1] in units:
return float(duration[:-1]) * units[duration[-1]]
else:
return float(duration)
duration = convert_to_seconds(sys.argv[1])
cmd = sys.argv[2:]
try:
if duration > 0:
proc = subprocess.Popen(cmd)
proc.communicate(timeout=duration)
else:
subprocess.run(cmd)
except subprocess.TimeoutExpired:
# Send SIGTERM and wait until subprocess terminates
proc.terminate()
proc.wait()
# Exit code 124 is used by GNU 'timeout' for timeout expiration
sys.exit(124)
" "$duration" "${cmd[@]}"
}
fi

move_files() {
local source_path=$1
local target_dirpath=$2
Expand Down

0 comments on commit 9ccc162

Please sign in to comment.