5

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
  • This might be a bit of an overkill, but maybe slurm will help you – Wayne_Yux Jan 28 '16 at 15:01
  • It seems indeed overkill but it's good to know it exists. – youri Jan 28 '16 at 15:07
  • I don't think it exists, but scripted everything can be done, e.g. create a command addcommand <command> to add a command to the (a) queue. Overkill? – Jacob Vlijm Jan 28 '16 at 15:09
  • 1
    Bunch of options at: superuser.com/q/220364/334516 – muru Jan 28 '16 at 17:53
  • Very interesting @muru. Your comment could be an answer to my question. Not all the suggested seem to actually work, but it's actually a good pointer. Thanks. – youri Jan 29 '16 at 15:16
2

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.

0

Although bash in and of itself doesn't have the "queue" or "stack" type of job control, but they do have job control (which has existed for years, way back to korn shell of Unix System V ) whereby you can put the processes in background, and control them with kill command

DIR:/coreutils
skolodya@ubuntu:$ gedit &> /dev/null &
[1] 26742

DIR:/coreutils
skolodya@ubuntu:$ gnome-terminal &> /dev/null &                                
[2] 26767

DIR:/coreutils
skolodya@ubuntu:$ jobs                                                         
[2] + Running              gnome-terminal >/dev/null 2>&1 
[1] - Running              gedit >/dev/null 2>&1 

DIR:/coreutils
skolodya@ubuntu:$ kill -SIGSTOP %1                                             

DIR:/coreutils
skolodya@ubuntu:$ jobs
[1] + Stopped (signal)     gedit >/dev/null 2>&1 
[2] - Running              gnome-terminal >/dev/null 2>&1 

DIR:/coreutils
skolodya@ubuntu:$ kill -SIGCONT %1                                             

DIR:/coreutils
skolodya@ubuntu:$ jobs                                                         
[1] + Stopped (signal)     gedit >/dev/null 2>&1 
[2] - Running              gnome-terminal >/dev/null 2>&1

The output of those commands in the above example goes to /dev/null, but of course if that's a command line app, you want to redirect output somewhere you can view it later. For instance, a named pipe.

DIR:/coreutils
skolodya@ubuntu:$ mkfifo test.fifo                                             

DIR:/coreutils
skolodya@ubuntu:$ apt-cache search 1> test.fifo &
[1] 27775

DIR:/coreutils
skolodya@ubuntu:$ cat test.fifo | head -n 3                                    
screen - terminal multiplexer with VT100/ANSI terminal emulation
screen-dbg - Debugging symbols for GNU Screen
asic0x-dkms - iBurst USB modem driver in DKMS format

Here I am redirecting stdout (file descriptor 1 ) to a pipe I created, and review it later. This can be done with multiple commands, by creating multiple named pipes.

Side note: One could script something with these options, at least IMHO, it's easier to just use these job control methods by themselves.

  • In fact . . . I might try to script something just for fun . . .but that's no promise – Sergiy Kolodyazhnyy Jan 31 '16 at 8:22
  • I don't think this answers the requirement for "the first command is not completed yet so it is stacked". The key point lies in the title: "Serialise shell commands". – youri Jan 31 '16 at 12:46

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.