Removing all at jobs except the ones running?

Bo Berglund bo.berglund at gmail.com
Mon Mar 7 16:05:30 UTC 2022


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

>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!

After some googling and trial-error work I have now wound up with this atremove
script:

#!/bin/bash
#Removes all pending at jobs
DEBUG="1"

CMD='atq -q = | cut -f 1'

IFS=$'\n' run_arr=($(eval "$CMD"))

if [ -n $run_arr ]; then #Check if here are running jobs
  for job in "${run_arr[@]}"
  do
    greparg="$greparg -e $job"
  done
  CMD="atq | cut -f 1 | grep -v $greparg | xargs atrm" #Exclude running jobs
else
  CMD="atq | cut -f 1 | xargs atrm" #No need to exclude running jobs
fi

if [ "$DEBUG" == "1" ]; then
  echo "Command: $CMD"
else
  eval "CMD"
fi
echo "Done"


It seems to work as far as I have tested, it is able to figure out the jobs that
are in running state and excluding these from the final atrm call.


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list