0

I'd love to "wrap" any command I type, such that, if it took more than 20 seconds to complete, I can run a notify command.

Something like:

$ sleep 30

Would conceptually do something like:

elapsed=real_time $(sleep 30);
if [ $elapsed > 20 ];
 say "Done";
fi

Is this possible?

2 Answers2

1

You can set a SIGALRM to fire after a specific amout of time, and cancel it when all your jobs have finished. This is tricky to do in pure shell script, though. The timeout command in GNU coreutils is a convenient and reasonably user-friendly starting point.

If you want this to be integrated into your interactive shell, you might be able to put it in a DEBUG trap. Perhaps see https://askubuntu.com/a/409801/25077

tripleee
  • 3,308
  • 5
  • 36
  • 35
1

If you have an idea that you're going to be running a long-running command, you can do something like:

sleep 30 && say "hi"

You don't want to manually add this for every command, but then again you really should know the difference between a quick ls command and a longer run brew install, make, etc, that you can expect to run for a while.

jimtut
  • 1,714