bash 为什么“at”命令总是警告我命令将通过 sh 执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/181241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does the "at" command always warn me that commands will be executed via sh?
提问by raldi
Every time I use the "at" command, I get this message:
每次使用“at”命令时,都会收到以下消息:
warning: commands will be executed using /bin/sh
What is it trying to warn me about? More importantly, how do I turn the warning off?
它想警告我什么?更重要的是,如何关闭警告?
回答by Jerub
It serves as a good warning to those of us that don't use bash as our shell, because we we'll forget that a feature that's in our day-to-day shell isn't going to be available when this code is run at the appointed time.
对于我们这些不使用 bash 作为我们的 shell 的人来说,这是一个很好的警告,因为我们会忘记,当这段代码运行时,我们日常 shell 中的功能将不可用在约定的时间。
i.e.
IE
username@hostname$ at 23:00
warning: commands will be executed using /bin/sh
at> rm **/*.pyc
at> <EOT>
job 1 at 2008-10-08 23:00
The use of '**' there is perfectly valid zsh, but not /sbin/sh! It's easy to make these mistakes if you're used to using a different shell, and it's your responsibility to remember to do the right thing.
'**' 的使用是完全有效的 zsh,但不是 /sbin/sh!如果您习惯于使用不同的 shell,就很容易犯这些错误,记住做正确的事是您的责任。
回答by Dave Sherohman
Does the warning have any harmful effect aside from being annoying? The man page doesn't mention any way of turning it off, so I don't think you can stop it from being emitted without rebuilding your atfrom source.
除了烦人之外,警告是否有任何有害影响?该名男子页面没有提到关闭它的任何方式,所以我不认为你可以被发射而无须重新创建阻止它在从源代码。
Now, if you want to just not see it, you can use at [time] 2>/dev/nullto send it off to oblivion, but, unfortunately, the at>prompts are printed to STDERR for some reason (a bug, IMO - they really should go to STDOUT), so they're also hidden by this.
现在,如果您不想看到它,您可以使用at [time] 2>/dev/null它来将其发送到遗忘,但不幸的at>是,由于某种原因,提示被打印到 STDERR(一个错误,IMO - 他们真的应该去 STDOUT),所以他们'也被这个隐藏了。
It may be possible to work up some shell plumbing which will eliminate the warning without also eating the prompts, but
可能会处理一些外壳管道,这将消除警告而不吃提示,但是
- my attempt at this (
at [time] 2>&1 | grep -v warning) doesn't work and - even if you can find a combination that works, it won't be suitable for aliasing (since the time goes in the middle rather than at the end), so you'll need to either type it in full each time you use it or else write a wrapper script around
atto handle it.
- 我在这方面的尝试 (
at [time] 2>&1 | grep -v warning) 不起作用,并且 - 即使你能找到一个有效的组合,它也不适合混叠(因为时间在中间而不是在最后),所以你需要在每次使用它时完整地输入它或者否则编写一个包装脚本
at来处理它。
So, unless it causes actual problems, I'd say you're probably best off just ignoring the warning like the rest of us.
因此,除非它引起实际问题,否则我认为您最好像我们其他人一样忽略警告。
回答by warren
If you wish to get around that message, have 'at' run a script that calls a specified environment, be it ksh, bash, csh, zsh, perl, etc.
如果您希望绕过该消息,请让“at”运行一个调用指定环境的脚本,无论是 ksh、bash、csh、zsh、perl 等。
addition- see the 'at' man page http://www.rt.com/man/at.1.htmlfor more information.
另外-有关更多信息,请参阅“at”手册页http://www.rt.com/man/at.1.html。
at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh.
回答by Matija Nalis
The source code for at.c(from Debian at 3.1.20-3 version) contains an answer:
at.c(来自 Debian 3.1.20-3 版本)的源代码包含一个答案:
/* POSIX.2 allows the shell specified by the user's SHELL environment variable, the login shell from the user's password database entry,
or /bin/sh to be the command interpreter that processes the at-job.
It also alows a warning diagnostic to be printed. Because of the
possible variance, we always output the diagnostic. */fprintf(stderr, "warning: commands will be executed using /bin/sh\n");
/* POSIX.2 允许由用户的 SHELL 环境变量指定的 shell、来自用户密码数据库条目的登录 shell
或 /bin/sh 作为处理 at-job 的命令解释器。
它还允许打印警告诊断。由于
可能存在差异,我们总是输出诊断结果。*/fprintf(stderr, "警告:命令将使用 /bin/sh\n");
You can work around it with shell redirection:
您可以使用 shell 重定向来解决它:
% echo "echo blah" | at now+30min 2>&1 | fgrep -v 'warning: commands will be executed using /bin/sh'
job 628 at Fri Mar 8 23:25:00 2019
Or you even create an function for your leisure use (for example for interactive shell in ~/.zshrcor ~/.profile):
或者,您甚至可以创建一个供您休闲使用的函数(例如,用于~/.zshrc或 中的交互式 shell ~/.profile):
at() {
/usr/bin/at "$@" 2>&1 | fgrep -v 'warning: commands will be executed using /bin/sh'
}
After that, it will not warn your about with that specific warning (while other warning/errors messages will still reach you)
之后,它不会用该特定警告警告您(而其他警告/错误消息仍会到达您)

