How do I print contents of at jobs?

53,592

Solution 1

at -c jobnumber will list a single job. If you want to see all of them, you might create a script like

#!/bin/bash
MAXJOB=$(atq | head -n1 | awk '{ print $1; }')
for each in $(seq 1 $MAXJOB); do echo "JOB $each"; at -c $each; done 

Probably there's a shorter way to do that, I just popped that out of my head :)

Solution 2

Building upon previous responses, this lists each job's line from atq showing job number and scheduled time and then just the command to be run, sorted chronologically (rather than job number):

for j in $(atq | sort -k6,6 -k3,3M -k4,4 -k5,5 |cut -f 1); do atq |grep -P "^$j\t" ;at -c "$j" | tail -n 2; done

producing, e.g.

48  Fri Mar 10 15:13:00 2017 a root
/usr/local/bin/a-command

47  Fri Mar 10 15:14:00 2017 a root
/usr/local/bin/another-command

Solution 3

A much simpler approach:

for j in $(atq | cut -f 1); do at -c "$j"; done

You could also look at each one in less in turn, which might be clearer:

for j in $(atq | cut -f 1); do at -c "$j" | less; done

Solution 4

I've created command atqc for this ("atq with command"). A bash function. Run this on the bash command line (terminal command). Or put it in the ~/.bashrc file to make it available for later:

atqc () { atq|perl -ne'($q,$j)=/((\d+).*)/;qx(at -c $j)=~/(marcinDEL\w+).\n(.*?)\n\1/s;print"$q $2"'; }

Test it:

atqc

That works for RHEL7 with at -V version 3.1.13.

Ubuntu 16.04 with at -V version 3.1.18 has a slightly different output format in at -c N, so on my Ubuntu server this works:

atqc(){ atq|perl -nE'($q,$j)=/((\d+).*)/;qx(at -c $j)=~/\n}\n(.*?)\s*$/s;say"$q: $1"';}

Solution 5

Here is an alternative approach, similar to the others in this thread but using awk more:

atq | awk '{ print "at -c " $1 }' | bash

To do a "dry run" and see what would be executed, simply remove the "| bash" part from the end.

Share:
53,592

Related videos on Youtube

che
Author by

che

Updated on September 17, 2022

Comments

  • che
    che over 1 year

    I have a Debian box with some jobs scheduled using at. I know I can list the jobs with their times using atq, but is there any way to print out their contents, apart from peeking into /var/spool/cron/atjobs?

  • Dennis Williamson
    Dennis Williamson almost 14 years
    at -c $(atq | cut -f 1) or for each in $(atq | cut -f 1) will avoid "Cannot find jobid" errors. (Also, Bash has for ((each=1; each<=MAXJOB; each++)) so no need for seq. If you're concerned with portability, then #!/bin/sh.)
  • norcalli
    norcalli about 5 years
    don't forget good old awk, atq | awk '{ system("at -c " $1) }'