Bash在后台在同一行中执行多个命令 [英] Bash executing multiple commands in background in same line

查看:368
本文介绍了Bash在后台在同一行中执行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行时

-bash-3.2$ cd /scratch/;nohup sh xyz.sh>>del.txt &;exit

我收到以下错误消息.

-bash:意外令牌';'附近的语法错误

-bash: syntax error near unexpected token `;'

我正在尝试使用nohup ..&运行独立的进程. . ';'对于除nohup sh xyz.sh>>del.txt &;

I am trying to run a detached process using nohup .. & . ';' works fine for all other commands except nohup sh xyz.sh>>del.txt &;

任何人都可以在这里告诉问题.谢谢

can anyone tell the problem here . Thanks

推荐答案

新答案: 如@CharlesDuffy正确标记;我的旧答案创建了一个subshel​​l,这完全没有必要.实际上,&符号也终止了该行.因此不再需要添加额外的;. Bash抱怨是因为仅包含;的一行不是有效命令. Bash将您的命令读取为:

New answer: as @CharlesDuffy correctly remarked; my old answer created a subshell, which is entirely unnecessary. In fact the & sign also terminates the line; so no more need to add an extra ;. Bash is complaining because a single line containing only a ; is not a valid command. Bash reads your command as:

cd /scratch/
nohup sh xyz.sh>>del.txt &
;
exit

因此,您应该只在nohup行之后删除;,并且(再次;感谢@CharlesDuffy);仅在成功进入/scratch/目录时才调用nohup命令;使用&&表示仅在第一个命令成功时才执行下一个命令.

Therefore you should just remove the ; after your nohup line, and (again; thanks @CharlesDuffy); only call the nohup command if you succeeded to enter the /scratch/ directory; using && means the next command is executed only if the first one succeeds.

cd /scratch/ && nohup sh xyz.sh>>del.txt & exit

旧答案:

如果您使用的是bash shell,则可以尝试将命令放在引号之间

You can try putting your command between quotes if you are in a bash shell

cd /scratch/ ; `nohup sh xyz.sh>>del.txt &` ; exit

您可以查看此​​问题

you can take a look at this question

这篇关于Bash在后台在同一行中执行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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