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
  • peak or peek? You want to see what its doing while its doing it? – Journeyman Geek Jan 7 '17 at 14:41
  • @JourneymanGeek, Oops. Peek would be what I meant to type. I want to see what it's doing. – Drew Chapin Jan 7 '17 at 14:43
  • Redirect the script output to a file when you schedule the at job and tail -f that file? – muru Jan 7 '17 at 14:44
  • @muru, Let's pretend I've already run the script without using tail and don't want to stop it. – Drew Chapin Jan 7 '17 at 14:45
  • I'd need to test but would modifying the script to use tee, and saving the output to a file, then tailing it work? – Journeyman Geek Jan 7 '17 at 14:46
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
  • 1
    You sir, are my hero! That worked perfectly! – Drew Chapin Jan 7 '17 at 15:01
  • 4
    I would like to add though, that if you know you're only running one at job. You don't even need the PID. You can just run sudo ls /var/spool/cron/atspool and it should, in theory be the only file there. – Drew Chapin Jan 7 '17 at 15:04
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.