通过 os.system 推送
我正在使用 crontab 为我的 Minecraft 服务器运行维护脚本。大多数时候它工作正常,除非 crontab 尝试使用重新启动脚本。如果我手动运行重新启动脚本,则不会有任何问题。因为我相信它与路径名有关,所以我试图确保它始终从minecraft目录执行任何minecraft命令。所以我将命令封装在 Pushd/popd 中:
os.system("pushd /directory/path/here")
os.system("command to sent to minecraft")
os.system("popd")
下面是一个交互式会话,将 Minecraft 排除在外。一个简单的“ls”测试。正如你所看到的,它根本不从 Pushd 目录运行 os.system 命令,而是从 /etc/ 运行命令,这是我运行 python 来说明我的观点的目录。显然,pushd 不能通过 python 工作,所以我想知道我还能如何实现这一目标。谢谢!
>>> def test():
... import os
... os.system("pushd /home/[path_goes_here]/minecraft")
... os.system("ls")
... os.system("popd")
...
>>> test()
~/minecraft /etc
DIR_COLORS cron.weekly gcrypt inputrc localtime mime.types ntp ppp rc3.d sasldb2 smrsh vsftpd.ftpusers
DIR_COLORS.xterm crontab gpm-root.conf iproute2 login.defs mke2fs.conf ntp.conf printcap rc4.d screenrc snmp vsftpd.tpsave
X11 csh.cshrc group issue logrotate.conf modprobe.d odbc.ini profile rc5.d scsi_id.config squirrelmail vz
adjtime csh.login group- issue.net logrotate.d motd odbcinst.ini profile.d rc6.d securetty ssh warnquota.conf
aliases cyrus.conf host.conf java lvm mtab openldap protocols redhat-release security stunnel webalizer.conf
alsa dbus-1 hosts jvm lynx-site.cfg multipath.conf opt quotagrpadmins resolv.conf selinux sudoers wgetrc
alternatives default hosts.allow jvm-commmon lynx.cfg my.cnf pam.d quotatab rndc.key sensors.conf sysconfig xinetd.conf
bashrc depmod.d hosts.deny jwhois.conf mail named.caching-nameserver.conf passwd rc rpc services sysctl.conf xinetd.d
blkid dev.d httpd krb5.conf mail.rc named.conf passwd- rc.d rpm sestatus.conf termcap yum
cron.d environment imapd.conf ld.so.cache mailcap named.rfc1912.zones pear.conf rc.local rsyslog.conf setuptool.d udev yum.conf
cron.daily exports imapd.conf.tpsave ld.so.conf mailman netplug php.d rc.sysinit rwtab shadow updatedb.conf yum.repos.d
cron.deny filesystems init.d ld.so.conf.d makedev.d netplug.d php.ini rc0.d rwtab.d shadow- vimrc
cron.hourly fonts initlog.conf libaudit.conf man.config nscd.conf pki rc1.d samba shells virc
cron.monthly fstab inittab libuser.conf maven nsswitch.conf postfix rc2.d sasl2 skel vsftpd
sh: line 0: popd: directory stack empty
=== (CentOS 服务器与 python 2.4)
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:
os.system("pushd /directory/path/here")
os.system("command to sent to minecraft")
os.system("popd")
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!
>>> def test():
... import os
... os.system("pushd /home/[path_goes_here]/minecraft")
... os.system("ls")
... os.system("popd")
...
>>> test()
~/minecraft /etc
DIR_COLORS cron.weekly gcrypt inputrc localtime mime.types ntp ppp rc3.d sasldb2 smrsh vsftpd.ftpusers
DIR_COLORS.xterm crontab gpm-root.conf iproute2 login.defs mke2fs.conf ntp.conf printcap rc4.d screenrc snmp vsftpd.tpsave
X11 csh.cshrc group issue logrotate.conf modprobe.d odbc.ini profile rc5.d scsi_id.config squirrelmail vz
adjtime csh.login group- issue.net logrotate.d motd odbcinst.ini profile.d rc6.d securetty ssh warnquota.conf
aliases cyrus.conf host.conf java lvm mtab openldap protocols redhat-release security stunnel webalizer.conf
alsa dbus-1 hosts jvm lynx-site.cfg multipath.conf opt quotagrpadmins resolv.conf selinux sudoers wgetrc
alternatives default hosts.allow jvm-commmon lynx.cfg my.cnf pam.d quotatab rndc.key sensors.conf sysconfig xinetd.conf
bashrc depmod.d hosts.deny jwhois.conf mail named.caching-nameserver.conf passwd rc rpc services sysctl.conf xinetd.d
blkid dev.d httpd krb5.conf mail.rc named.conf passwd- rc.d rpm sestatus.conf termcap yum
cron.d environment imapd.conf ld.so.cache mailcap named.rfc1912.zones pear.conf rc.local rsyslog.conf setuptool.d udev yum.conf
cron.daily exports imapd.conf.tpsave ld.so.conf mailman netplug php.d rc.sysinit rwtab shadow updatedb.conf yum.repos.d
cron.deny filesystems init.d ld.so.conf.d makedev.d netplug.d php.ini rc0.d rwtab.d shadow- vimrc
cron.hourly fonts initlog.conf libaudit.conf man.config nscd.conf pki rc1.d samba shells virc
cron.monthly fstab inittab libuser.conf maven nsswitch.conf postfix rc2.d sasl2 skel vsftpd
sh: line 0: popd: directory stack empty
===
(CentOS server with python 2.4)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
pushd
和popd
有一些附加功能:它们将以前的工作目录存储在堆栈中 - 换句话说,您可以pushd
五次,执行一些操作东西,然后popd
五次以结束你开始的地方。您在这里没有使用它,但对于其他搜索此类问题的人来说可能会很有用。您可以通过以下方式进行模拟:pushd
andpopd
have some added functionality: they store previous working directories in a stack - in other words, you canpushd
five times, do some stuff, andpopd
five times to end up where you started. You're not using that here, but it might be useful for others searching for the questions like this. This is how you can emulate it:我不认为你可以在
os.system()
调用中调用pushd
:也许你的系统实际上提供了
pushd
二进制文件触发 shell 内部函数(我想我以前在 FreeBSD 上见过这个FreeBSD有一些类似这样的技巧,但不适用于pushd
),但进程的当前工作目录不会受到影响由其他进程 - 所以你的第一个system()
启动一个 shell,运行一个假设的pushd
,启动一个 shell,运行ls
,启动一个shell,运行一个假设的popd
...它们都不会互相影响。您可以使用
os.chdir("/home/path/")
来更改路径:http://docs.python.org/library/os.html#os-file-dirI don't think you can call
pushd
from within anos.system()
call:Maybe just maybe your system actually provides a
pushd
binary that triggers a shell internal function (I think I've seen this on FreeBSD beforeFreeBSD has some tricks like this, but not forpushd
), but the current working directory of a process cannot be influenced by other processes -- so your firstsystem()
starts a shell, runs a hypotheticalpushd
, starts a shell, runsls
, starts a shell, runs a hypotheticalpopd
... none of which influence each other.You can use
os.chdir("/home/path/")
to change path: http://docs.python.org/library/os.html#os-file-dir无需使用
pushd
——只需使用os.chdir
:No need to use
pushd
-- just useos.chdir
:或者创建一个与“with”一起使用的类
Or make a class to use with 'with'
如果你确实需要一个堆栈,即如果你想做几次pushd和popd,
参见上面的naught101。
如果没有,只需执行以下操作:
If you really need a stack, i.e. if you want to do several pushd and popd,
see naught101 above.
If not, simply do:
在 Python 2.5 及更高版本中,我认为更好的方法是使用上下文管理器,如下所示:
然后您可以像下面这样使用它:
通过使用上下文管理器,您将异常并返回值安全:您的代码将始终 cd back到它开始的地方,即使您抛出异常或从上下文块内部返回。
您还可以在嵌套块中嵌套 Pushd 调用,而不必依赖全局目录堆栈:
In Python 2.5 and later, I think a better method would be using a context manager, like so:
You can then use it like the following:
By using a context manager you will be exception and return value safe: your code will always cd back to where it started from, even if you throw an exception or return from inside the context block.
You can also nest pushd calls in nested blocks, without having to rely on a global directory stack:
每个 shell 命令都在单独的进程中运行。它生成一个 shell,执行 pushd 命令,然后 shell 退出。
只需在同一个 shell 脚本中编写命令即可:
更好的(也许)方法是使用
subprocess
< /a> 模块:Each shell command runs in a separate process. It spawns a shell, executes the pushd command, and then the shell exits.
Just write the commands in the same shell script:
A nicer (perhaps) way is with the
subprocess
module: