3

I have a simple Bash script I am running to parallelize and automate the execution of a program written in Sage MATH:

#!/bin/bash
for i in {1..500}; do
  echo Spinning up threads...
  echo Round $i
  for j in {1..8}; do
    ../sage ./loader.sage.py &
  done
  wait
done 2>/dev/null

I would like to add a timeout so that on each thread, after 5 seconds,

../sage ./loader.sage.py &

will timeout, kill the thread, and continue execution. How would I go about doing this? Apologies in advance if this is a noob question, I can't seem to get the syntax right. I am running this in a Ubuntu WSL. The program I am calling is written in Python and run through the Sage MATH interpreter which liaises to Singular.

  • 1
    timeout 5 ../sage ./loader.sage.py & As long as you have coreutils, but in that case you could probably simplify this whole thing with GNU parallel – jesse_b Jan 11 at 15:37
  • I'm preparing to run this on a compute cluster and am trying to avoid dependencies, but I'll look into it, thanks! – Alex Williams Jan 11 at 15:38
6

Using GNU Parallel:

parallel --timeout 5 -j 8 -N0 ../sage ./loader.sage.py ::: {1..4000} 2>/dev/null

This will execute ../sage ./loader.sage.py 4000 times, 8 jobs at a time, each with a timeout of 5 seconds

From the parallel man page:

--timeout duration
           Time out for command. If the command runs for longer than duration seconds it will get killed as per --termseq.

Note: This command replaces your entire loop.

| improve this answer | |
0

man timeout -- in my Linux Mint but you don't declare your OS.

timeout 5 ../sage ./loader.sage.py &

Added some debug to show processes start and stop. It seems possible your sage startup is disk-bound (maybe having to clear cache) and cannot use all the CPU available. As Python is (AFAIK) a just-in-time compile, that may be a bottleneck.

#!/bin/bash

for i in {1..3}; do
    echo Spinning up threads...
    echo Round $i
    for j in {1..3}; do
        timeout 1.5 sleep 60 &
    done
    echo "==== $( date '+%H:%M:%S.%N') Before"; jobs
    ps -f
    wait
    echo "==== $( date '+%H:%M:%S.%N') After"; jobs
    ps -f
    echo; timeout 1.0 sleep 0.2
done
| improve this answer | |
  • I've seen this solution before, I'm not sure why but it seems to not work. I'll add some clarification to the original question, but my CPU usage drops from 100% to about 30% and doesn't move on. – Alex Williams Jan 11 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.