How do I print contents of at jobs?

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"';}