Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushd through os.system

Tags:

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)

like image 669
Thomas Thorogood Avatar asked May 31 '11 22:05

Thomas Thorogood


People also ask

What does the pushd command do?

The pushd command saves the current working directory in memory so it can be returned to at any time, pushd moves to the parent directory. The popd command returns to the path at the top of the directory stack. This directory stack is accessed by the command dirs in Unix or Get-Location -stack in Windows PowerShell.

What is pushd in Python?

pushd.py. """A Python context to move in and out of directories. Copyright 2016-2020, Howard Hamilton. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and. associated documentation files (the "Software"), to deal in the Software without restriction, in-

Does pushd create directory?

pushd adds a directory to the top of the stack and popd removes a directory from the top of the stack. To display directories in the directory stack (or history), we can use the dirs command as shown.

What is pushd bash command?

To store the current directory information in the stack before moving to another directory location, `pushd` command is used in bash. This command works on LIFO (Last In First Out) based. This means, the directory information will be stored at the end of the stack location.


1 Answers

In Python 2.5 and later, I think a better method would be using a context manager, like so:

import contextlib import os   @contextlib.contextmanager def pushd(new_dir):     previous_dir = os.getcwd()     os.chdir(new_dir)     try:         yield     finally:         os.chdir(previous_dir) 

You can then use it like the following:

with pushd('somewhere'):     print os.getcwd() # "somewhere"  print os.getcwd() # "wherever you started" 

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:

with pushd('somewhere'):     # do something     with pushd('another/place'):         # do something else     # do something back in "somewhere" 
like image 133
spiralman Avatar answered Oct 12 '22 22:10

spiralman