How To Add Linux Commands To The Queue And Execute Them One By One
Today, I came across a coolest Linux command line utility called “Task Spooler”. As the name implies, Task spooler is a Unix batch system that can be used to add the Linux commands to the queue and execute them one after the other in numerical order (ascending order, to be precise). Please do not confuse it with ‘at’ command, which is used to execute Linux commands at a specified time. Unlike at command, Task spooler runs a command immediately from the queue as soon as the previous command is finished.
This can be very useful when you have to run a lots of commands, but you don’t want to waste time waiting for one command to finish and run the next command. You can queue it all up and Task Spooler will execute them one by one. In the mean time, you can do other activities. Each user in 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 use, give a lot of output, or for whatever reason it’s better not to run them at the same time. In a nutshell, Task Spooler is a command line program to queue up other commands for batch execution.
In this brief tutorial, let me show you how to install and use Task Spooler in Unix-like operating systems.
Add Linux Commands To The Queue And Execute Them One By One Using Task Spooler
Installing Task Spooler
On Debian, Ubuntu, Linux Mint:
Task Spooler is available in the default repositories of Debian, Ubuntu and other DEB based systems. So, you can install it using command:
sudo apt-get install task-spooler
For other systems, you can download the Task Spooler source file from this link and build it as native package to the Linux distribution you use and install it as explained in any one of the following methods.
Let us see some practical examples. All examples provided here are tested in Ubuntu 16.04 LTS system.
Note: In Debian/Ubuntu Task Spooler can be executed with ‘tsp’ command, because there is an another program with same name called ts (Time Stamping Authority tool (client/server). For Linux distributions other than Ubuntu/Debian, you can run it using ‘ts’ command.
Run tsp command:
Right now, there is nothing in the queue. Allow me to add some commands to the queue. To do so, run:
tsp echo Welcome OSTechNix
tsp echo "Hello World"
Now, run tsp command again to view the commands in the queue:
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 Welcome OSTechNix 1 finished /tmp/ts-out.8H6LLB 0 0.00/0.00/0.00 echo Hello World
As you see in the above output, each command has a unique ID in (0, 1, 2.. etc.) in ascending order. Also, it shows the current of state of commands (Eg. finished or running) in the queue. The echo commands are very simple and short, so we got the result as ‘finished’. Let us run a some command that takes more time to finish.
tsp find / -type f -printf '%T %pn' | sort | head -n 20
This command will find and display the top 20 oldest files in the root (/) file system.
Now, run tsp command to view the list of commands in the queue.
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 Welcome OSTechNix 1 finished /tmp/ts-out.8H6LLB 0 0.00/0.00/0.00 echo Hello World
As you see in the above output, the command with ID 2 is running. Similarly, you can add as many as commands you want to run using Task Spooler.
To view the output of a running job to check what’s going on, enter the following command:
tsp -c 2
Here, 2 is the ID of running command. press CTRL C to return back to the Terminal. it doesn’t cancel the running command. It will only taek you back to the Terminal. The job will still run on the back ground.
To clear all commands which are finished from the queue, run:
Here, C is capital. The above command will clear the last completed commands from the queue. It will not remove any running commands or the commands in the queue.
To remove commands from the queue, run it with job ID like below:
Also, you can remove a command (running, finished, queued up) by specifying the ID like below.
tsp -r 2
The above command will remove the command that has ID 2 from the queue.
Remember, you need to run Task Spooler in distributions other than Debian/Ubuntu using ts command.
For more details, refer the man pages.
If you’re too lazy to wait for a command to complete, you can queue it all up using Task Spooler. It will execute commands from the queue one by one in ascending order. You can view the output of any running command using its ID at any time. It will not run all commands at once. Instead, it will run commands one after another. Task Spooler can be used for executing batch jobs.
And, that’s all for now. If you find this guide useful, please share it on your social, professional networks and support us. I will be soon here with another interesting guide. Until then, stay tuned with OSTechNix.