Home Command line utilities How To Add Linux Commands To The Queue And Execute Them One By One

How To Add Linux Commands To The Queue And Execute Them One By One

By sk
Published: Updated: 8.7K views

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.

Install 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.

Add Linux Commands To The Queue And Execute Them One By One Using Task Spooler

Let us see some practical examples. All examples provided here are tested in Ubuntu 18.04 LTS system.

Note: In Debian/Ubuntu systems, Task Spooler should 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:

$ tsp

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:

$ tsp

Sample output:

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. Take a look at the following command:

$ 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 add the above command to queue:

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

Sample output:

2

Now, run tsp command to view the list of commands in the queue.

$ tsp

Sample output:

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
Add Linux Commands To The Queue Using Task Spooler

Add Linux Commands To The Queue Using Task Spooler

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.

Update:

As one of our reader mentioned in the comment section, the find command should be run like below:

$ tsp sh -c "find Downloads/ -type f -printf '%T+ %p\n' | sort | head -n 20"

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 won't cancel the running command. It will only take you back to the Terminal. The job will still run on the back ground.

You can remove a command (running, finished, queued up) from the queue using -r flag followed by the ID like below.

$ tsp -r 2

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

To clear all commands from the queue, simply run:

$ tsp -C

Please note that 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.

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

Or,

$ man tsp

Suggested read:


Conclusion

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.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

5 comments

Claudio June 9, 2017 - 1:44 pm

What about cmd1; cmd2; cmdN?

Reply
SK June 10, 2017 - 12:32 pm

You’re right. These commands would be executed in the current shell. I think one of the purposes of task spooler is to allow jobs to be decoupled from the submitting shell.

Reply
Nicholas Barnes May 7, 2020 - 1:31 am

Running:
–> tsp find / -type f -printf ‘%T+ %p\n’ | sort | head -n 20
on my Debian box only seems to run
–> tsp find / -type f -printf ‘%T+ %p\n’
as the output file has the full output of the find command unsorted.
It seems that you can only pipe if you run the command through “sh -c”, so
–> tsp sh -c “find / -type f -printf ‘%T+ %p\n’ | sort | head -n 20”
Does work.

Reply
sk May 7, 2020 - 1:22 pm

Thansk for pointing it out. I updated the guide.

Reply
Aamir Aman September 15, 2020 - 10:45 pm

how to get the complete history, because after restarting (due to electricity) it does not show any states of finished or running jobs
your help will be appreciated

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More