pushd through os.system(通过 os.system 推送)
问题描述
我正在使用 crontab 为我的 minecraft 服务器运行维护脚本.大多数时候它工作正常,除非 crontab 尝试使用重启脚本.如果我手动运行重新启动脚本,则没有任何问题.因为我相信它与路径名有关,所以我试图确保它总是从 minecraft 目录执行任何 minecraft 命令.所以我将命令封装在 pushd/popd 中:
I'm using a crontab to run a maintenance script for my minecraft server. Most of the time it works fine, unless the crontab tries to use the restart script. If I run the restart script manually, there aren't any issues. Because I believe it's got to do with path names, I'm trying to make sure it's always doing any minecraft command FROM the minecraft directory. So I'm encasing the command in pushd/popd:
下面是一个交互式会话,将我的世界从等式中剔除.一个简单的ls"测试.如您所见,它根本没有从 pushd 目录运行 os.system 命令,而是从/etc/运行 python 来说明我的观点的目录.显然 pushd 不能通过 python 工作,所以我想知道我还能如何实现这一目标.谢谢!
Below is an interactive session taking minecraft out of the equation. A simple "ls" test. As you can see, it does not at all run the os.system command from the pushd directory, but instead from /etc/ which is the directory in which I was running python to illustrate my point.Clearly pushd isn't working via python, so I'm wondering how else I can achieve this. Thanks!
===(CentOS 服务器与 python 2.4)
=== (CentOS server with python 2.4)
推荐答案
每个 shell 命令都在单独的进程中运行.它生成一个 shell,执行 pushd 命令,然后 shell 退出.
Each shell command runs in a separate process. It spawns a shell, executes the pushd command, and then the shell exits.
只需在同一个 shell 脚本中编写命令:
Just write the commands in the same shell script:
更好的(也许)方法是使用 subprocess
模块:
A nicer (perhaps) way is with the subprocess
module:
这篇关于通过 os.system 推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!