How To Add Linux Commands To The Queue And Execute Them One By One
Today, I stumbled upon a cool Linux command line utility called “Task Spooler”. As the name says, 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 utility 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 them 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.
- How To Build Linux Packages For Multiple Platforms Easily
- How To Build Packages From Source Using CheckInstall
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. Let us 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+ %p\n' | 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+ %p\n 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:
$ tsp -C
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.
$ man ts
$ man tsp
I find it Task spooler very useful when I have to run multiple commands. I am just a lazy fellow to wait for one command to finish and execute the another one. Using Task spooler, I queued up the list of commands to be executed and it executed commands from the queue one by one in ascending order. I can also view the output of any running command using its ID at any time. Please be mindful that It won’t run all commands at once. Instead, it will run commands one after another. That said, Task Spooler is opt for 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. More good stuffs to come. Stay tuned!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!