53

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?

che
  • 679
  • 1
  • 5
  • 13

5 Answers5

56

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 :)

Janne Pikkarainen
  • 31,454
  • 4
  • 56
  • 78
  • 7
    `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`.) – Dennis Williamson Aug 26 '10 at 15:08
  • don't forget good old `awk`, `atq | awk '{ system("at -c " $1) }'` – norcalli Apr 21 '19 at 23:13
11

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
chicks
  • 3,639
  • 10
  • 26
  • 36
palilicium
  • 111
  • 1
  • 2
4

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
Wildcard
  • 153
  • 2
  • 14
2

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"';}
Kjetil S.
  • 181
  • 3
1

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.

presto8
  • 111
  • 2