Removing all at jobs except the ones running?

Bo Berglund bo.berglund at gmail.com
Mon Mar 7 10:50:23 UTC 2022


On Mon, 07 Mar 2022 20:11:18 +1100, Karl Auer <kauer at biplane.com.au> wrote:

>On Mon, 2022-03-07 at 09:12 +0100, Bo Berglund wrote:
>> I am scheduling jobs to be executed using at and this works fine
>> mostly.
>> 
>> But sometimes I want to reset the at job list and I have found this
>> command to
>> ease this when the at job list is long:
>> 
>> atq | cut -f 1 | xargs atrm
>> [...]
>> But since atq lists also the *running* jobs it will attempt to remove
>> also these, which I don't want.
>> How can I exclude running jobs from removal while still being able to
>> remove all queued jobs easily?
>
>atq lists all jobs by default, but you can limit the output to a named
>queue. If you specify the special queue-name "=" it will list only
>running jobs. So if you combine the list of all jobs and the list of
>running jobs, then use sort and "uniq -u" to remove any jobs that
>appear twice, you should get a list of non-running jobs that you can
>pump into xargs.
>
>Of course, any job can go from running to non-running at any moment, or
>vice versa.
>
>Be warned that I have not tried this myself!

I tested as follows:

$ atq -q =
260     Mon Mar  7 10:59:00 2022 = pi

and

$ atq
257     Mon Mar  7 19:58:00 2022 a pi
260     Mon Mar  7 10:59:00 2022 = pi
268     Mon Mar  7 18:59:00 2022 a pi
273     Tue Mar  8 00:05:00 2022 a pi
267     Mon Mar  7 17:59:00 2022 a pi
256     Mon Mar  7 15:58:00 2022 a pi
266     Mon Mar  7 16:59:00 2022 a pi
264     Mon Mar  7 14:59:00 2022 a pi
271     Mon Mar  7 21:59:00 2022 a pi
255     Mon Mar  7 11:58:00 2022 a pi
263     Mon Mar  7 13:59:00 2022 a pi
261     Mon Mar  7 11:59:00 2022 a pi
258     Mon Mar  7 23:58:00 2022 a pi
265     Mon Mar  7 15:59:00 2022 a pi
262     Mon Mar  7 12:59:00 2022 a pi
270     Mon Mar  7 20:59:00 2022 a pi
272     Mon Mar  7 23:59:00 2022 a pi
269     Mon Mar  7 19:59:00 2022 a pi

so now I need a command which will remove what is output from "atq -q =" from
the output of atq without argument...

I am pretty dumb when dealing with bash scripting, so this is probably over the
top of me.
I could write a special executable in my preferred dev language to do it but it
seems to be excessive for such a simple task...

I tried this in my script atremove but the output is really confusing:

#!/bin/bash
#Removes all pending at jobs
CMD='atq -q = | cut -f 1'
echo "Cmd: $CMD"
RUNNING=$($CMD)
echo "Running: $RUNNING"
eval "atq | cut -f 1 | grep -v $RUNNING"

But when I run this I get the strange output below:

$ atremove
Cmd: atq -q = | cut -f 1
atq: invalid option -- 'f'
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
       atrm [-V] job ...
       batch
Running:
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

The echoed comamand which seemingly fails actually works to extract the running
job as shown when I run the echoed command on the command line:

$ atq -q = | cut -f 1
260

This is exactly what I want to stuff into the RUNNING variable, namely the value
260.
This in turns should be used by grep.

The final command should then be as follows, which actually works if typed in:
$ atq | cut -f 1 | grep -v 260
257
268
273
267
256
266
264
271
255
263
261
258
265
262
270
272
269

Here I have the list of non-executing jobs (but it still does not work for
multiple exclusions, just one executing job)

Two problems:

1) Why does the first command not work when called in the script by using
RUNNING=$($CMD)

2) Why does this command not work from within the script:
eval "atq | cut -f 1 | grep -v $RUNNING"


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list