在bash函数中生成后台进程 [英] Spawn a background process in a bash function

查看:51
本文介绍了在bash函数中生成后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Bash函数来启动需要从某个文件夹启动的服务器,但是我不想启动该服务器影响我的当前工作.我写了以下内容:

I am working on writing a Bash function to start a server that needs to be started from a certain folder, but I don't want starting this server to impact my current work. I've written the following:

function startsrv {
        pushd .
        cd ${TRUNK}
        ${SERVERCOMMAND} & 
        popd
}

我的变量都已设置,但是执行此命令时,我收到有关输出中意外分号的错误,看来Bash在&符号后的分号后以 $ {SERVERCOMMAND} 插入分号.背景.

My variables are all set, but when this executes, I get an error regarding an unexpected semicolon in the output, it appears that Bash is inserting a semicolon after the ampersand starting ${SERVERCOMMAND} in the background.

在仍然使用push和popd确保我回到当前目录的同时,我还能做些什么来在后台启动 $ {SERVERCOMMAND} 吗?

Is there anything I can do to start ${SERVERCOMMAND} in the background while still using pushd and popd to make sure I end up back in my current directory?

echo $ {SERVERCOMMAND} 的输出,因为已被请求:

Output of echo ${SERVERCOMMAND}, since it was requested:

yeti --server --port 8727

错误消息:

-bash: syntax error near unexpected token `;'

推荐答案

$ SERVERCOMMAND 的值是什么?您必须在其中添加分号.

What is the value of $SERVERCOMMAND? You must have a semi-colon in it.

对于它值得的是,您可以将push/cd简化为一个pushed:

For what it's worth you can simplify the pushd/cd to one pushd:

pushd $TRUNK
$SERVERCOMMAND &
popd

或创建一个子外壳,使cd仅影响一个命令:

Or create a subshell so the cd only affects the one command:

(cd $TRUNK; $SERVERCOMMAND &)

这篇关于在bash函数中生成后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆