/
pdfalto_server
executable file
·56 lines (47 loc) · 1.27 KB
/
pdfalto_server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Timeout.
timeout=20 # 20 seconds
# Interval between checks if the process is still alive.
interval=1
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
delay=0
# Default limit for the memory usage by this bash script
memory_limit=6242304
command=${0:0:${#0}-7}
args=("$@")
pdfalto_params=()
for ((n=0; n<="$#";n++)); do
case ${args[n]} in
--timeout)
timeout=${args[n+1]}
((n++))
;;
--ulimit)
memory_limit=${args[n+1]}
((n++))
;;
*)
pdfalto_params+=" ${args[n]}"
;;
esac
done
#echo timeout $timeout
#echo pdfalto commands: $command $pdfalto_params
#echo memory_limit $memory_limit
# Limit the memory usage by this bash script
ulimit -Sv $memory_limit
# 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 $command $pdfalto_params