如何在bash脚本中运行一个命令,该命令在脚本退出时不会停止?
我有一个运行其他命令的 BASH 脚本,我希望保持它们运行,以防主脚本停止。我尝试使用 &
运行这些命令,但没有帮助。我做错了什么吗?
I have a BASH script which runs other commands and I would like to keep them running in case the main script stops. I tried to run those commands with &
but it didn't help. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是正常的:shell 的作业控制机制在退出时终止其作业。
至少有两种解决方案:
disown
;at
安排稍后的工作;现在,问题是您想如何处理在后台启动的命令的输出:
disown
,您会丢失它;使用disown
,您会丢失它;at
,stdout 和 stderr 通过邮件发送给您(但如果您愿意,您可以重定向 stdout/stderr);nohup
,您可以自己定义输出文件。This is normal: the shell's job control mechanism terminates its jobs when it exits.
There are at least two solutions:
disown
after you have launched a job;at
to schedule your job at a later time;nohup
command.Now, there is the question of what you want to do with the output of the command you launch in the background:
disown
, you lose it;at
, stdout and stderr are sent to you by mail (but then you can redirect stdout/stderr if you want);nohup
, you define the output file yourself.尝试在命令之前使用 nohup(代表不挂起):
默认情况下,它会将命令的标准输出转发到名为 nohup.out 的文件中。
Try using nohup (stands for no hangup) before your command:
It will forward the stdout of the command into a file called nohup.out by default.
尝试使用“nohup”命令来运行COMMAND,忽略挂断信号
Try 'nohup' command to run COMMAND, ignoring hangup signals
以下是如何执行此操作的示例:
Here is an example how to do this: