简体   繁体   English

如何在VM上无限期运行Shell脚本?

[英]How to run shell script on VM indefinitely?

I have a VM that I want running indefinitely. 我有要无限期运行的VM。 The server is always running but I want the script to keep running after I log out. 服务器始终处于运行状态,但是我希望脚本在注销后继续运行。 How would I go about doing so? 我将如何去做? Creating a cron job? 创建一份工作?

In general the following steps are sufficient to convince most Unix shells that the process you're launching should not depend on the continued existence of the shell: 通常,以下步骤足以说服大多数Unix shell,您正在启动的进程不应依赖于shell的持续存在:

  • run the command under nohup nohup下运行命令
  • run the command in the background 在后台运行命令
  • redirect all file descriptors that normally point to the terminal to other locations 将通常指向终端的所有文件描述符重定向到其他位置

So, if you want to run command-name , you should do it like so: 因此,如果要运行command-name ,则应这样进行:

nohup command-name >/dev/null 2>/dev/null </dev/null &

This tells the process that will execute command-name to send all stdout and stderr to nowhere (instead of to your terminal) and also to read stdin from nowhere (instead of from your terminal). 这告诉将执行command-name的进程将所有stdoutstderr发送到任何地方(而不是发送到您的终端),也从任何地方(而不是从终端)读取stdin Of course if you actually have locations to write to/read from, you can certainly use those instead -- anything except the terminal is fine: 当然,如果您确实有要写入/读取的位置,则可以肯定地使用这些位置-除终端之外的任何设备都可以:

nohup command-name >outputFile 2>errorFile <inputFile &

See also the answer in Petur's comment, which discusses this issue a fair bit. 另请参阅Petur的评论中的答案,该评论对这个问题进行了一些讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM