且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash在后台在同一行中执行多个命令

更新时间:2023-11-22 13:33:52

新答案: 如@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