如何无限期地运行服务器脚本

发布于 2024-08-15 20:55:14 字数 354 浏览 4 评论 0原文

我想在远程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

带上头具痛哭 2024-08-22 20:55:14

您基本上要问的是“如何创建守护进程?”你想要做的是“守护进程”,网络上有很多这样的例子。该过程基本上是 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/

瑾兮 2024-08-22 20:55:14

如果您可以修改脚本,则可以捕获 SIGHUP 信号并避免使用 nohup。在 bash 脚本中,您可以编写:

trap " echo ignoring hup; " SIGHUP

您可以采用相同的技术来终止程序:例如,在处理程序中捕获 SIGUSR1 信号,设置一个标志,然后从主循环中正常退出。这样您就可以发送您选择的信号以可预测的方式停止您的程序。

If you can modify the script, then you can catch SIGHUP signals and avoid the need for nohup. In a bash script you would write:

trap " echo ignoring hup; " SIGHUP

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.

浮生未歇 2024-08-22 20:55:14

在某些情况下,您想要在远程计算机/服务器上执行/启动某些脚本(将自动终止)并与服务器断开连接。

例如:在盒子上运行的脚本,执行时 1) 获取模型并将其复制到集群(远程服务器) 2) 创建一个脚本,用于使用 wodel 运行模拟并将其推送到服务器 3) 在集群上启动脚本服务器并断开连接 4) 由此启动的脚本的职责是在服务器中运行模拟,完成后(需要几天时间才能完成)将结果复制回客户端。

我会使用以下命令:

ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'

例如:

echo '#!/bin/bash  
rm -rf statuslist  
mkdir statuslist  
chmod u+x ~/monitor/concat.sh  
chmod u+x ~/monitor/script.sh  
nohup ./monitor/concat.sh &  
' > script.sh 

chmod u+x script.sh

rsync -azvp script.sh remotehost:/tmp

ssh remoteshot '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'

希望这有帮助;-)

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:

ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'

eg:

echo '#!/bin/bash  
rm -rf statuslist  
mkdir statuslist  
chmod u+x ~/monitor/concat.sh  
chmod u+x ~/monitor/script.sh  
nohup ./monitor/concat.sh &  
' > script.sh 

chmod u+x script.sh

rsync -azvp script.sh remotehost:/tmp

ssh remoteshot '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'

Hope this helps ;-)

遮云壑 2024-08-22 20:55:14

如果您想(或必须)避免更改脚本本身,这是最简单的方法。如果脚本始终像这样运行,您可以编写一个包含您刚刚键入的行的迷你脚本并运行它。 (或使用别名,如果合适的话)

回答你第二个问题:

 $ nohup ./test &
[3] 11789
 $ Sending output to nohup.out

$jobs
[1]-  Running                 emacs *h &
[3]+  Running                 nohup ./test &
$ kill %3
$ jobs
[1]-  Running                 emacs *h &
[3]+  Exit 143                nohup ./test

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:

 $ nohup ./test &
[3] 11789
 $ Sending output to nohup.out

$jobs
[1]-  Running                 emacs *h &
[3]+  Running                 nohup ./test &
$ kill %3
$ jobs
[1]-  Running                 emacs *h &
[3]+  Exit 143                nohup ./test

Ctrl+c works too, (sends a SIGINT) as does kill (sends a SIGTERM)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文