💾 Archived View for thrig.me › blog › 2023 › 11 › 11 › a-small-at-gotcha.gmi captured on 2024-08-18 at 18:09:21. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-11-14)

-=-=-=-=-=-=-

A Small at(1) Gotcha

The gotcha may not be obvious,

"The working directory, the environment (except for the variables BASH_VERSINFO, DISPLAY, EUID, GROUPS, PPID, SHELLOPTS, SSH_AGENT_PID, SSH_AUTH_SOCK, TERM, TERMCAP, UID, and _), and the umask are retained from the time of invocation."

http://man.openbsd.org/man1/at.1

but may become apparent if the working directory the job was scheduled in is no longer available. Thus, a safe default when you do not care about the working directory would be to chdir to a "safe" directory before entering the at job.

    $ ( cd / && echo echo echo | at +1m )
    ...

Test

A test script would run something like the following: remove (or deny permission to) the working directory the test code runs under.

    #!/bin/sh
    set -e
    d=`mktemp -d`
    cd "$d"
    f=`mktemp`
    printf '(pwd;ls;cd /tmp;pwd;ls) | tee "%s"' "$f" |
    at +1m
    rmdir "$d"      # whoops!
    #chmod 000 "$d" # whoops?
    sleep 61
    printf '%s\n' "$f"

atdirtest.sh

    $ sh atdirtest.sh
    commands will be executed using /bin/ksh
    job 1699666500.c at Sat Nov 11 01:35:00 2023
    /tmp/tmp.4ncZGxKiQ1
    $ cat /tmp/tmp.4ncZGxKiQ1
    $

The job did not run at all; assuming local mail is setup (it may not be, or may be broken, or could be accumulating to some long neglected /var/mail file that now has a few million entries in it)† there will be a mail message with an error. Maybe.

    Your "at" job on gear.thrig.me
    "/var/cron/atjobs/1699666500.c"

    produced the following output:

    sh: <stdin>[39]: cd: /tmp/tmp.YEpGSEczxB - No such file or directory
    Execution directory inaccessible

Example Use

What use is at? I use it for scheduling short-term reminders

    #!/bin/sh
    #   tellme tea +2m
    #   tellme show 11:59
    x11=${DISPLAY?"DISPLAY is not set"}
    msg=${1?"message at-timespec"}
    shift

    cd          # go home to avoid PWD being removed

    printf 'DISPLAY="%s" xembiggen "%s"\n' "$x11" "$msg" |
    at "$@" 2>/dev/null || echo >&2 "at failed"

that others might use a phone or something like remind for.

https://dianne.skoll.ca/projects/remind/

Safe Directories

The root directory (/) is typical for daemons. Directories like $HOME could go away or be unavailable, especially if they are on remote-mounted filesystems, and the remote end or the network goes down. Other directories may make more sense depending on what the subsequent code expects and how the system is setup.

Endnote

† One time the database admins finally fixed the mail transport agent, and my pager got hit with a backlog of 900+ messages, which set the pager to ringing for a while, until the pager company deactivated it, which made being on-call slightly more difficult than usual, and I had to call up the pager company and tell them to reactivate the pager, after cleaning out the mail spool. Why people who didn't know how to run a mail server were trying to run a mail server is probably a good question.

Yes, this was back when pagers were a thing. No, I haven't ever owned a smartphone.

tags #at #unix