如何无限期地运行服务器脚本
我想在远程 Linux 服务器上无限期地运行异步程序。该脚本不会向服务器本身输出任何内容(除了偶尔将信息写入 mysql 数据库之外)。到目前为止,我能找到的唯一选项是 nohup 命令:
nohup script_name &
据我了解,nohup 允许该命令运行,即使在我注销 SSH 会话后,同时出现“&”字符让命令在后台运行。我的问题很简单:这是做我想做的事情的最佳方式吗?我只是尝试长时间运行一个脚本,同时偶尔停止它进行更新。
另外,如果 nohup 确实是最佳选择,那么当我需要时终止脚本的正确方法是什么?对于终止 nohup 进程的最佳方法似乎存在一些分歧。
谢谢
I would like to run an asynchronous program on a remote linux server indefinitely. This script doesn't output anything to the server itself(other than occasionally writing information to a mysql database). So far the only option I have been able to find is the nohup command:
nohup script_name &
From what I understand, nohup allows the command to run even after I log out of my SSH session while the '&' character lets the command run in the background. My question is simple: is this the best way to do what I would like? I am only trying to run a single script for long periods of time while occasionally stopping it to make updates.
Also, if nohup is indeed the best option, what is the proper way to terminate the script when I need to? There seems to be some disagreement over what is the best way to kill a nohup process.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您基本上要问的是“如何创建守护进程?”你想要做的是“守护进程”,网络上有很多这样的例子。该过程基本上是
fork()
,子进程创建一个新会话,父进程退出,子进程复制并关闭控制终端(STDIN、STDOUT、STDERR)的打开文件句柄。有一个可用的软件包可以为您完成所有这些操作,名为 python-daemon 。
要执行正常关闭,请查看信号库来创建信号处理程序。
此外,在网络上搜索“python daemon”将带来常见 C 守护进程的许多重新实现:http ://code.activestate.com/recipes/66012/
What you are basically asking is "How do I create a daemon process?" What you want to do is "daemonize", there are many examples of this floating around on the web. The process is basically that you
fork()
, the child creates a new session, the parent exits, the child duplicates and then closes open file handles to the controlling terminal (STDIN, STDOUT, STDERR).There is a package available that will do all of this for you called python-daemon.
To perform graceful shutdowns, look at the signal library for creating a signal handler.
Also, searching the web for "python daemon" will bring up many reimplementations of the common C daemonizing process: http://code.activestate.com/recipes/66012/
如果您可以修改脚本,则可以捕获
SIGHUP
信号并避免使用nohup
。在 bash 脚本中,您可以编写:您可以采用相同的技术来终止程序:例如,在处理程序中捕获 SIGUSR1 信号,设置一个标志,然后从主循环中正常退出。这样您就可以发送您选择的信号以可预测的方式停止您的程序。
If you can modify the script, then you can catch
SIGHUP
signals and avoid the need fornohup
. In a bash script you would write:You can employ the same technique to terminate the program: catch, say, a SIGUSR1 signal in a handler, set a flag and then gracefully exit from your main loop. This way you can send this signal of your choice to stop your program in a predictable way.
在某些情况下,您想要在远程计算机/服务器上执行/启动某些脚本(将自动终止)并与服务器断开连接。
例如:在盒子上运行的脚本,执行时 1) 获取模型并将其复制到集群(远程服务器) 2) 创建一个脚本,用于使用 wodel 运行模拟并将其推送到服务器 3) 在集群上启动脚本服务器并断开连接 4) 由此启动的脚本的职责是在服务器中运行模拟,完成后(需要几天时间才能完成)将结果复制回客户端。
我会使用以下命令:
例如:
希望这有帮助;-)
There are some situations when you want to execute/start some scripts on a remote machine/server (which will terminate automatically) and disconnect from the server.
eg: A script running on a box which when executed 1) takes a model and copies it to a custer (remote server) 2) creates a script for running a simulation with the wodel and push it to server 3) starts the script on the server and disconnect 4) The duty of the script thus started is to run the simulation in the server and once completed (will take days to complete) copy the results back to client.
I would use the following command:
eg:
Hope this helps ;-)
如果您想(或必须)避免更改脚本本身,这是最简单的方法。如果脚本始终像这样运行,您可以编写一个包含您刚刚键入的行的迷你脚本并运行它。 (或使用别名,如果合适的话)
回答你第二个问题:
Ctrl+c 也可以(发送 SIGINT),就像kill(发送 SIGTERM)一样
That is the simplest way to do it if you want to (or have to) avoid changing the script itself. If the script is always to be run like this, you can write a mini script containing the line you just typed and run that instead. (or use an alias, if appropriate)
To answer you second question:
Ctrl+c works too, (sends a SIGINT) as does kill (sends a SIGTERM)