Skip to content

Commit 2d969f2

Browse files
authored
Merge pull request #23 from attogram/test-timeout
I added a pure-bash `run_with_timeout` function, exported it, and rep…
2 parents c83b627 + fafb255 commit 2d969f2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# create-overview.sh
3+
# tour-the-gallery.sh
44
#
55
# This script creates an overview of all screensavers.
66
#
@@ -18,6 +18,27 @@ check_deps() {
1818
fi
1919
}
2020

21+
run_with_timeout() {
22+
local timeout_duration=$1
23+
shift
24+
local command_to_run=("$@")
25+
26+
# Run the command in the background
27+
"${command_to_run[@]}" &
28+
local cmd_pid=$!
29+
30+
# Sleep for the duration and then kill the command
31+
(sleep "$timeout_duration" && kill "$cmd_pid" 2>/dev/null) &
32+
local sleeper_pid=$!
33+
34+
# Wait for the command to finish, suppressing job termination message
35+
wait "$cmd_pid" 2>/dev/null
36+
37+
# Kill the sleeper process if it's still running
38+
kill "$sleeper_pid" 2>/dev/null
39+
}
40+
41+
2142
# Create a title card as a .cast file
2243
# $1: text to display
2344
# $2: output .cast file
@@ -66,6 +87,7 @@ main() {
6687
all_casts+=("$temp_dir/00_intro.cast")
6788

6889
# 2. Loop through screensavers
90+
export -f run_with_timeout
6991
local i=1
7092
for screensaver_dir in "$gallery_dir"/*/; do
7193
if [[ -d "$screensaver_dir" ]]; then
@@ -82,7 +104,7 @@ main() {
82104

83105
# Record a longer snippet
84106
local temp_cast="$temp_dir/$(printf "%02d" $i)_${name}_temp.cast"
85-
asciinema rec --command="bash -c 'timeout 6s env SHELL=/bin/bash $run_script'" --overwrite "$temp_cast"
107+
asciinema rec --command="bash -c 'run_with_timeout 6s env SHELL=/bin/bash $run_script'" --overwrite "$temp_cast"
86108

87109
# Cut a snippet from the middle
88110
local snippet_cast="$temp_dir/$(printf "%02d" $i)_${name}_snippet.cast"

0 commit comments

Comments
 (0)