在 nohup 中使用别名

发布于 2025-01-05 12:03:34 字数 409 浏览 2 评论 0原文

为什么以下不起作用?

$ 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

(我问这个问题的原因是因为我已经将我的 perlpython 别名为不同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国粹 2025-01-12 12:03:34

因为 shell 不会将别名传递给子进程(除非您使用 $() 或 ``)。

$ alias sayHello='/bin/echo "Hello world!"'

现在这个 shell 进程中已知了一个别名,这很好,但只能在这个 shell 进程中工作。

$ sayHello 

Hello world!

由于您在同一个 shell 中说了“sayHello”,所以它起作用了。

$ nohup sayHello

这里,程序“nohup”作为子进程启动。因此,它不会收到别名。
然后它启动子进程“sayHello” - 未找到该进程。

对于您的具体问题,最好使新的“perl”和“python”尽可能看起来像普通的。我建议设置搜索路径。

在您的 ~/.bash_profile 添加

export PATH="/my/shiny/interpreters/bin:${PATH}"

然后重新登录。

由于这是一个环境变量,因此它传递给所有子进程,无论它们是否为 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.

$ sayHello 

Hello world!

Since you said "sayHello" in the same shell it worked.

$ nohup sayHello

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 add

export PATH="/my/shiny/interpreters/bin:${PATH}"

Then 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.

碍人泪离人颜 2025-01-12 12:03:34

对于 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.

燃情 2025-01-12 12:03:34

使用 bash,您可以使用 -i 选项以交互方式调用子 shell。这将获取您的 .bashrc 并启用 expand_aliases shell 选项。当然,只有在您的 .bashrc 中定义了您的别名(这是约定)时,这才有效。

Bash 手册页:

如果存在 -i 选项,则 shell 是交互式

expand_aliases:如果设置,别名将按照上述 ALIASES 下的描述进行扩展。对于交互式 shell,此选项默认启用

当启动非登录 shell 的交互式 shell 时,bash 会读取并执行来自 /etc/bash.bashrc~/.bashrc 的命令,如果这些命令文件存在。


$ nohup bash -ci 'sayHello'

With bash, you can invoke a subshell interactively using the -i option. This will source your .bashrc as well as enable the expand_aliases shell option. Granted, this will only work if your alias is defined in your .bashrc which is the convention.

Bash manpage:

If the -i option is present, the shell is interactive.

expand_aliases: If set, aliases are expanded as described above under ALIASES. This option is enabled by default for interactive shells.

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.


$ nohup bash -ci 'sayHello'
姐不稀罕 2025-01-12 12:03:34

如果您查看 Bash 手册的别名部分,它说

检查每个简单命令的第一个单词(如果未加引号)以查看
如果它有别名。

不幸的是, bash 似乎没有像 zsh 的 全局别名,在任意位置展开。

If you look at the Aliases section of the Bash manual, it says

The first word of each simple command, if unquoted, is checked to see
if it has an alias.

Unfortunately, it doesn't seem like bash has anything like zsh's global aliases, which are expanded in any position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文