60

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
  • 749

6 Answers6

64

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

14

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,915
  • 10
  • 29
  • 37
5

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
  • 182
3

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.
  • 201
2

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
  • 121
  • 2
0

I wanted to have my at jobs listed by execution date plus their corresponding command line. Swap the ## at line get_at_line if you prefer the output separated by blanks instead of columns.

me@PC3540:~$ which atl  
/home/me/bin/atl  
me@PC3540:~$ cat /home/me/bin/atl

#!/bin/bash
PFIX=A00000
get_at_line () {
for i in $(at -l | cut -f1 )
do
ATLINE=$(at -l | grep ^$i)
ATSTATUS=$(echo $ATLINE | tr -s " " | cut -f7 -d" ")
ATTIME=$(echo $ATLINE | cut -f2- | cut -f1-6 -d" ")
ATISO=$(date -d "$ATTIME" +%Y-%m-%d' '%R)
ATCMD=$(at -c $i | sed -n -e '/}/,$p' | grep -v -e '}' )
HEX=$(printf '%x\n' $i)
VAL=$(printf "%X\n" $((0x$PFIX+0x$HEX)))
VAL=${VAL,,}
echo Job: $(printf "%02d\n" $i) $VAL At: $ATISO $ATSTATUS: $ATCMD
done
}
if $(which at > /dev/null)
then
if [ ! -z "$(at -l)" ]
then
get_at_line | sort -k5 -k6 | column -t -s" " -n
##get_at_line | sort -k5 -k6
else
echo -e "\n.. no at jobs found for user $USER\n"
fi
else
echo -e "\n.. service at not installed \n"
fi

fission
  • 3,761