在 nohup 中使用别名
为什么以下不起作用?
$ alias sayHello='/bin/echo "Hello world!"'
$ sayHello
Hello world!
$ nohup sayHello
nohup: appending output to `nohup.out'
nohup: cannot run command `sayHello': No such file or directory
(我问这个问题的原因是因为我已经将我的 perl
和 python
别名为不同的 perl/python 二进制文件,这些二进制文件是为了我自己的目的而优化的;但是,nohup 给了我如果我不提供 perl/python 二进制文件的完整路径,则会出现麻烦)
Why doesn't the following work?
$ alias sayHello='/bin/echo "Hello world!"'
$ sayHello
Hello world!
$ nohup sayHello
nohup: appending output to `nohup.out'
nohup: cannot run command `sayHello': No such file or directory
(the reason I ask this question is because I've aliased my perl
and python
to different perl/python binaries which were optimized for my own purposes; however, nohup gives me troubles if I don't supply full path to my perl/python binaries)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为 shell 不会将别名传递给子进程(除非您使用 $() 或 ``)。
$ alias sayHello='/bin/echo "Hello world!"'
现在这个 shell 进程中已知了一个别名,这很好,但只能在这个 shell 进程中工作。
由于您在同一个 shell 中说了“sayHello”,所以它起作用了。
这里,程序“nohup”作为子进程启动。因此,它不会收到别名。
然后它启动子进程“sayHello” - 未找到该进程。
对于您的具体问题,最好使新的“perl”和“python”尽可能看起来像普通的。我建议设置搜索路径。
在您的
~/.bash_profile
添加然后重新登录。
由于这是一个环境变量,因此它将传递给所有子进程,无论它们是否为 shell - 它现在应该经常工作。
Because the shell doesn't pass aliases on to child processes (except when you use $() or ``).
$ alias sayHello='/bin/echo "Hello world!"'
Now an alias is known in this shell process, which is fine but only works in this one shell process.
Since you said "sayHello" in the same shell it worked.
Here, a program "nohup" is being started as a child process. Therefore, it will not receive the aliases.
Then it starts the child process "sayHello" - which isn't found.
For your specific problem, it's best to make the new "perl" and "python" look like the normal ones as much as possible. I'd suggest to set the search path.
In your
~/.bash_profile
addThen re-login.
Since this is an environment variable, it will be passed to all the child processes, be they shells or not - it should now work very often.
对于 bash:尝试执行 nohup '
your_alias
'。这对我有用。我不知道为什么没有显示反引号。将您的别名放在反引号内。For bash: Try doing nohup '
your_alias
'. It works for me. I don't know why back quote is not shown. Put your alias within back quotes.使用 bash,您可以使用
-i
选项以交互方式调用子 shell。这将获取您的.bashrc
并启用expand_aliases
shell 选项。当然,只有在您的.bashrc
中定义了您的别名(这是约定)时,这才有效。Bash 手册页:
With bash, you can invoke a subshell interactively using the
-i
option. This will source your.bashrc
as well as enable theexpand_aliases
shell option. Granted, this will only work if your alias is defined in your.bashrc
which is the convention.Bash manpage:
如果您查看 Bash 手册的别名部分,它说
不幸的是, bash 似乎没有像 zsh 的 全局别名,在任意位置展开。
If you look at the Aliases section of the Bash manual, it says
Unfortunately, it doesn't seem like
bash
has anything likezsh
's global aliases, which are expanded in any position.