8

Let's say I run a script with at on Linux (example below) that I know is going to take several hours, or maybe even multiple days. I'm accustomed to at sending an email with the output of the command once it completes, but what if I wanted to peek at the current output before it completes? Let's also assume I've already run the script and forgot to redirect the output using tail, tee or other similar methods and do not want to stop the script. Is there anyways to do this?

drew@anubis:~$ at now
warning: commands will be executed using /bin/sh
at> ./myscript.sh
at> <EOT>
job 3 at Sat Jan  7 09:31:00 2017
Drew Chapin
  • 6,270

2 Answers2

9

If the job has started, and you have some way of obtaining the PID of that job, you could see where at is saving the output:

$ at now
warning: commands will be executed using /bin/sh
at> sleep 10m
at> <EOT>
job 7 at Sat Jan  7 20:18:00 2017
$ pgrep sleep
7582
$ ls -l /proc/7582/fd
total 0
lr-x------ 1 muru muru 64 Jan  7 20:19 0 -> /var/spool/cron/atjobs/a0000701795998 (deleted)
lrwx------ 1 muru muru 64 Jan  7 20:19 1 -> /var/spool/cron/atspool/a0000701795998
lrwx------ 1 muru muru 64 Jan  7 20:19 2 -> /var/spool/cron/atspool/a0000701795998

As you can see, the output is saved to a temp file, which you can now check:

$ sudo tail -f /var/spool/cron/atspool/a0000701795998
Subject: Output from your job        7
To: muru

You need sudo because the containing directory is not world-accessible (at least on Ubuntu 14.04):

$ sudo namei -lx /var/spool/cron/atspool/a0000701795998
f: /var/spool/cron/atspool/a0000701795998
Drwxr-xr-x root     root   /
drwxr-xr-x root     root   var
drwxr-xr-x root     root   spool
drwxr-xr-x root     root   cron
drwxrwx--T daemon   daemon atspool
-rw------- muru     muru   a0000701795998
muru
  • 1,336
0

Another way to determine which spool file belongs to your job is to get the job number from atq.

drew@sokar:~$ atq
38      Tue Jul 10 12:15:00 2018 = drew

Convert the job number to hex. e.g. 38 (dec) = 26 (hex).

drew@sokar:~$ printf "%x\n" 38
26

Your file will be be named Queue letter (99% of the time, it's a) followed by 5 digit hex job id (I learned this by looking through the source code of the at command).

drew@sokar:~$ sudo ls -l /var/spool/cron/atspool
total 55968
-rw------- 1 drew drew       49 Feb 16  2017 a0001c017a3cb2
-rw------- 1 drew drew 57297586 Feb 26  2017 a0001e017a7405
-rw------- 1 drew drew     2329 Jul 10 12:21 a0002601856a0f
                                              -----

Then you can view the stdout

drew@sokar:~$ sudo less /var/spool/cron/atspool/a0002601856a0f
Drew Chapin
  • 6,270