TL;DR: Move sudo
across the pipe (|
) to the at
command itself.
You haven't really been running at
as root.
You ran cat
as root (sudo cat ...
), and piped the output of cat
to at
, but you did not run the at
command itself as root.
On many systems, some non-root users are permitted to run at jobs. However, if that's not what you want, your system is configured the way you like, and you intend to run this particular at job as root, then you can solve the problem by running at
as root instead of cat
. (Either way, you do not need to run cat
as root in this situation.)
To do that, put sudo
before at
instead of cat
:
cat << EOF | sudo at 'now + 1 minute'
Optional details:
Currently you have:
sudo cat << EOF | at 'now + 1 minute'
echo "hello"
EOF
That runs cat
as root with sudo
, passing echo "hello"
to it (with here-document syntax). The cat
command's standard output is piped to the command at 'now + 1 minute'
, which is not run as root. Therefore at
attempts to schedule the job as the non-root user who ran it (you).
If you want the at job to run as root, you can fix this problem by changing it to:
cat << EOF | sudo at 'now + 1 minute'
echo "hello"
EOF
If you later configure a non-root user account that you're using to be permitted to run at jobs, then you would run neither command as root:
cat << EOF | at 'now + 1 minute'
echo "hello"
EOF
sleep 1m && echo "hello"
will be printed just fine... – Dec 28 '16 at 01:42at
. As far asat
could know, that terminal might not be appropriate for such use (or not exist) at the time the command runs. Instead, the output is mailed to you. But in Ubuntu -- unlike Debian -- no mail transfer agent is set up by default, so you don't receive the output at all. Do you need standard output from at jobs to go to your terminal, or is this just for testing? Either way, do any of these answers help? – Eliah Kagan Dec 28 '16 at 01:50at
(even if the session was terminated from any reason). – Dec 28 '16 at 02:08at
? Is running a subshell as a background job with&
--(sleep 1m && echo "hello")&
-- sufficient for your needs? (You could replace1m
with something like5s
for testing, of course.) – Eliah Kagan Dec 28 '16 at 02:12nohup sleep command
will fail on reboot though: http://unix.stackexchange.com/questions/332313/how-to-timeout-by-hours-in-bash-and-keep-timing-out-even-if-the-session-was-clo/333004?noredirect=1#comment586568_333004 Or I might miss understand roaima... – Dec 28 '16 at 02:26