How to queue Linux commands and execute them one at a time

Today we came across a command line utility in Linux called “Task Spooler” … As the name suggests, the Task spooler is a Unix batch system that can be used to queue Linux commands and execute them one by one in order (ascending, to be precise). Please do not confuse it with the command “At”which is used to execute Linux commands at a given time. Unlike the team at, Task spooler runs a command immediately from the queue as soon as the previous command has finished.

it can be very useful when you have many commands to execute, but you don’t want to waste time waiting for one command to finish and run the next command. You can queue all of them and Task Spooler will execute them one at a time. At the same time, you can do other activities. Each user on each system has his / her own job queue. It is also very useful when you know that your commands depend on a lot of RAM, a lot of disk usage, a lot of output, or for whatever reason, it is best not to run them at the same time. In a nutshell, Task Spooler is a command line program for queuing up other commands to execute a package.

In this quick guide, we’ll show you how to install and use Task Spooler on Unix-like operating systems.

Queue Linux commands and execute them one at a time with Task Spooler

Installing Task Spooler

On Debian, Ubuntu, Linux Mint:

Task Spooler is available in the default repository of Debian, Ubuntu and other DEB based systems. Thus, you can install it using the command:

sudo apt-get install task-spooler

For other systems, you can download the original Task Spooler file from this link and build it as a native package on Linux and install it.

Application

Let’s see some practical examples. All examples shown here were tested on Ubuntu 16.04 LTS.

Note: On Debian / Ubuntu, Task Spooler can be executed with the “tsp” command because there is another program with the same name called ts (Time Stamping Authority tool (client / server)). For other Linux distributions, you can start it with the ts command.

Run the tsp command:

tsp

Right now, there is nothing in the queue. Let me add some commands to the queue. To do this, run the following command:

tsp echo Добро пожаловать в AndreyEx
tsp echo "Привет, Мир"

Now run the tsp command again to view the queued commands:

tsp

Output example:

ID State Output E-Level Times(r/u/s) Command [run=0/1]
0 finished /tmp/ts-out.jpHIG1 0 0.01/0.00/0.00 echo Добро пожаловать в AndreyEx
1 finished /tmp/ts-out.8H6LLB 0 0.00/0.00/0.00 echo Привет, Мир

As you can see, in the above output, each command has a unique ID (0, 1, 2 .. etc.) in ascending order. It also shows the status of the commands (eg finished or running) in the queue. The echo command is very simple and short, so we got the result as “finished”. Let’s run some command that takes longer to complete.

tsp find / -type f -printf '%T+ %pn' | sort | head -n 20

This command will search and display the last 20 old files in the root filesystem (/).

Output example:

2

Now run the tsp command to see the list of queued commands.

tsp

Output example:

ID State Output E-Level Times(r/u/s) Command [run=1/1]
2 running /tmp/ts-out.79rMXn find / -type f -printf %T+ %pn
0 finished /tmp/ts-out.jpHIG1 0 0.01/0.00/0.00 echo Добро пожаловать в AndreyEx
1 finished /tmp/ts-out.8H6LLB 0 0.00/0.00/0.00 echo Привет, Мир

As you can see in the above output, the command with ID 2 is working. Alternatively, you can add as many commands as you want to run with Task Spooler.

To view the output of a running job to check what is happening, enter the following command:

tsp -c 2

Here 2 is the ID of the command run. press CTRL + C to go back to terminal. this does not override a running command. This will only take you back to the terminal. The work will continue to take place in the background.

To clear all commands that have ended from the queue, run the following command:

tsp -C

Here, C is an uppercase letter. This command will remove the last completed commands from the queue. This will not delete all running commands or commands in the queue.

To remove commands from the queue, run it with the job ID as shown below:

Alternatively, you can remove the command (running, finished, queued up) by specifying the ID as shown below.

tsp -r 2

The above command will remove the command that has ID 2 from the queue.

Remember that you need to start Task Spooler other than Debian / Ubuntu distributions using the command ts

For more details see the reference:

man ts

or

man tsp

Conclusion

If you are too lazy to wait for the command to complete, you can enqueue the whole thing with Task Spooler… It will execute commands from the queue one at a time in ascending order. You can view the result of any running commands using your ID at any time. It will not run all commands at once. Instead, it will execute commands one by one. The spooler task can be used to run batch jobs.

And it’s all. If you found this guide helpful, please share it on social media or write a comment.

Sidebar