Ubuntu – Serialise shell commands

bashcommand line

I'm looking for a way to "stack" commands from the terminal prompt so that only one at the time is executed:

  • First enter a command, press enter, execution starts, back to shell (like &)
  • I can enter a second command. I press enter, the first command is not completed yet so it is stacked (unlike &). But again, the prompt is back.

Kind of a mix between:

  • & to launch in the background, but the second command would run at the same time as the first one, and I don't want that;
  • && or ; to launch one command after the other, but you have to write them all in once. I want to regain the prompt after the command is executed or stacked.

The closest I've found would be:

$ cmd1 &
$ wait; cmd2 &

But I'm looking for something a bit more potent, allowing me to view pending, failed and completed commands. Maybe something like:

$ stackit cmd1
cmd1 started
$ stack it cmd2
cmd2 queued
$ stack it cmd3
cmd3 queued
$ stack it --
[1] running
[2] queued
[3] queued

Maybe to even allow for some parallelism, like 2 commands at the time.

This seems fairly generic so I don't feel like re-inventing the wheel.

Use cases:

  • Copying files to and from an old NAS that suffers when several operations run at the same time
  • wget on a large number of files where a limited number of connections would be preferred

Best Answer

You are searching for the task spooler command. In the Debian/Ubuntu's repositories, the pacakage to install is task-spooler and the binary to call is tsp.

In your case (&& style) you could use

tsp -d cmd1
tsp -d cmd2
tsp -d cmd3

then use

tsp -l

to inspect the status of the queue.

There are ways (-S) to increase the number of max simultanious jobs, too.