如何运行后台进程并且不等待?

2024-01-02

我的目标很简单:启动 rsync 并且不要等待。

Debian 上的 Python 2.7.9

示例代码:

rsync_cmd = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1)
rsync_cmd2 = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3} &".format(remote_user, remote_server, file1, file1)
rsync_path = "/usr/bin/rsync"
rsync_args = shlex.split("-a -e 'ssh -i /home/mysuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1))
#subprocess.call(rsync_cmd, shell=True)     # This isn't supposed to work but I tried it
#subprocess.Popen(rsync_cmd, shell=True)    # This is supposed to be the solution but not for me
#subprocess.Popen(rsync_cmd2, shell=True)   # Adding my own shell "&" to background it, still fails
#subprocess.Popen(rsync_cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)  # This doesn't work
#subprocess.Popen(shlex.split(rsync_cmd))   # This doesn't work
#os.execv(rsync_path, rsync_args)           # This doesn't work
#os.spawnv(os.P_NOWAIT, rsync_path, rsync_args) # This doesn't work
#os.system(rsync_cmd2)                      # This doesn't work
print "DONE"

(我注释掉执行命令只是因为我实际上将所有试验保留在代码中,以便我知道我做了什么和没有做什么。显然,我会使用正确的方式运行脚本行未注释。)

发生的事情是这样的......我可以在服务器上观看传输,当传输完成时,我会在屏幕上打印“完成”。

我希望发生的是在发出后立即打印“完成”rsync命令并开始传输。

看起来非常简单。我遵循了其他帖子中概述的详细信息,例如this https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python一和this https://stackoverflow.com/questions/89228/calling-an-external-command-in-python/2251026#2251026一个,但有些东西阻止它为我工作。

提前致谢。

(我已经尝试了 StackExchange 中能找到的所有内容,但不觉得这是重复的,因为我仍然无法让它工作。我的设置有些不对劲,需要帮助。)


以下是经过验证的 Python REPL 示例:

>>> import subprocess
>>> import sys
>>> p = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(100)'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT); print('finished')
finished

如何通过另一个终端窗口验证这一点:

$ ps aux | grep python

Output:

user           32820   0.0  0.0  2447684   3972 s003  S+   10:11PM   0:00.01 /Users/user/venv/bin/python -c import time; time.sleep(100)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何运行后台进程并且不等待? 的相关文章

随机推荐

  • 未找到快速命令

    由于某种原因 在我的机器上全局安装 Express 后npm install g express if I cd进入目录并尝试运行express 我收到以下错误 express command not found 即使我运行它sudo我仍然
  • AssertionError:将使用不支持的标签('cp310','cp310','linux_x86_64')构建轮子

    当我尝试使用 Python 3 10 安装 numpy 时 我收到此消息 如何解决这个问题 Copying numpy egg info to build bdist linux x86 64 wheel numpy 1 19 3 py3
  • 使用 Node.js、流和 Promise 下载文件

    这是我的代码片段 var processListing function directoryItems console log foreach var itemsToDownload directoryItems forEach funct
  • 骨干.js https

    整个早上都在谷歌上搜索 但找不到答案 官方文档中甚至没有 https 或 ssl 字样 目前我有类似的东西 var A backbone Collection extend url a 有没有办法让 url https 而不使用绝对路径 我
  • 如何避免 uitextfield 旁边的点

    当文本大于 uitextfield 的宽度时 是否可以删除 uitextfield 旁边显示的点 如果您调用以下代码使键盘可见 这是可能的 self yourTextField 成为FirstResponder 然后较大的文本从左侧被剪切
  • Airflow - 获取 dag run 的开始时间

    是否有可能获得气流中 dag 的实际开始时间 我所说的开始时间是指 dag 的第一个任务开始运行的确切时间 我知道我可以使用宏来获取执行日期 如果使用trigger dag运行作业 这就是我所说的开始时间 但如果作业按每日计划运行 那么 e
  • 无法在中心显示材质 ui 小吃栏

    我有一个材料 ui 小吃栏 我需要将其显示在屏幕中间 下面是我试图传递的属性 使其显示在屏幕中心 但显示在屏幕顶部 anchorOrigin vertical center horizontal center https codesandb
  • 出现错误:/bin/sh scriptcs:找不到命令

    我正在使用 Visual Studio Code for Mac 运行扩展 CodeRunner 我有一个简单的程序 using System namespace HelloWorldApplication class HelloWorld
  • “X-UA-兼容”内容=“IE = 9; IE = 8; IE = 7; IE = EDGE”

    这句话究竟是什么意思呢 一些例子使用 分离 IE 的版本 而有些则使用 哪个是对的 命令IE 9 IE 8 IE 7 IE EDGE有一定的重要性 我想知道这一点 Edit 我在用 如果您支持 IE 对于 Internet Explorer
  • 获取 Jasper Reports 表中第一行和最后一行的第一个字母

    I am using JasperSoft Studio in Eclipse to generate a signup sheet for a church The page header has the label of what la
  • 从 python 列表中删除 '\n' 列表项

    我有一个列表 其中包含一个项目 n 我想删除它 但是 删除命令不起作用 谁能告诉我我做错了什么 def main list1 ng g ng g g n n ns ns s n nd d nd nd d d d n n print list
  • 提交时将表单数据发送到 Javascript

    好吧 我觉得这应该很简单 所以要么我完全错过了这里和我读过的其他网站上的问题的要点 要么没有在相同的上下文中被问到 我有一个非常简单的表单元素 如下
  • 创建 XSS 易受攻击的网页

    我想创建一个 XSS 易受攻击的网页 该网页执行在输入框中输入的脚本 我在这里编写了这段代码 但每当我输入脚本时 什么也没有发生 p You wrote p
  • 使用 matplotlib 和 seaborn 创建圆形密度图

    我正在尝试创建下面两个图像的组合 密度图 https i stack imgur com kV09a png 圆形条形图 https i stack imgur com tc7eG png 基本上我想要密度图的设计 但围绕一个圆圈而不是正常
  • Android:模拟器出厂数据重置菜单选项不起作用

    我正在尝试测试我自己创建的新 Android 系统级应用程序 该应用程序在执行设备出厂重置后捕获各种系统事件挂钩 通过 Android Studio AVD Manager 我创建了多个模拟 Android TV 设备 包括 Oreo 8
  • 数据框中每个唯一组合的频率

    在数据集中 N 6000 我想分析 15 个虚拟 变量组合出现的频率 ID Var1 Var2 Var3 Var15 1 1 0 0 1 2 0 1 1 1 3 1 0 0 0 6000 1 0 0 0 对于这个例子 我希望看到的是组合 1
  • 当使用字符串时,C++ 程序会出现运行时错误

    include
  • 如何运行 Visual Studio (2017) 安装程序?

    一旦你知道答案 它可能真的很明显 但我无法在任何地方找到它 我不是在谈论making安装程序 我指的是运行安装程序 让我可以修改安装的 Visual Studio 2017 的哪些功能 主屏幕如下所示 我需要的屏幕是这个 在 Visual
  • Javamail 附加多个文件

    以下代码也使用 javamail api 通过 gmail smtp 服务器发送邮件和附件 public void doSendGmail from txtFrom getText password new String txtPasswo
  • 如何运行后台进程并且不等待?

    我的目标很简单 启动 rsync 并且不要等待 Debian 上的 Python 2 7 9 示例代码 rsync cmd usr bin rsync a e ssh i home myuser ssh id rsa 0 1 2 3 for