Re: Problem with subprocess.call and windows schtasks

Discussion in 'Python' started by Chris Rebert, Nov 18, 2012.

  1. Chris Rebert

    Chris Rebert Guest

    On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin <> wrote:
    > Hi,
    > I have this code:
    >
    > #!\Python27\python
    >
    > import subprocess
    > #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
    > Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
    > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    > /ST 07:50'], shell=True)
    > With either call, I get this error:
    > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    > The system cannot find the path specified.
    >
    > If I remove the ", shell=True" I get this:
    > C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    > C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
    > line 4, in <module>
    > subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    > /ST 07:50'])
    > File "C:\Python27\lib\subprocess.py", line 493, in call
    > return Popen(*popenargs, **kwargs).wait()
    > File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
    > errwrite)
    > File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
    > startupinfo)
    > WindowsError: [Error 2] The system cannot find the file specified
    > The file exists in said directory. I can execute it from the cmd prompt.


    Per the docs (http://docs.python.org/2/library/subprocess.html#frequently-used-arguments
    ):
    "If passing a single string [as the `args` argument], either `shell`
    must be True (see below) or else the string must simply name the
    program to be executed **without specifying any arguments.**"
    (emphasis mine)

    > So I tried this:
    > pgm = "SchTasks"
    > args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
    > #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
    > 07:50']
    > cmd = [pgm]
    > cmd.extend(args)
    > subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
    > but got this error:
    > ERROR: Invalid argument/option - <<the above argument>>
    >
    > If I use the other args list I get this error:
    > ERROR: Invalid argument/option - '/SC ONCE'
    > so apparently it liked the first argument.
    >
    > Please advise.


    Your tokenization of your command is incorrect. Consult the Note box
    in the docs regarding `args` tokenization, and apply it to your
    command:
    http://docs.python.org/2/library/subprocess.html#subprocess.Popen

    The-docs-are-your-friend-ly Yours,
    Chris
     
    Chris Rebert, Nov 18, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cct

    schtasks problem

    cct, Nov 12, 2003, in forum: ASP .Net Security
    Replies:
    0
    Views:
    180
  2. Tim Golden
    Replies:
    0
    Views:
    179
    Tim Golden
    Nov 18, 2012
  3. Dave Angel
    Replies:
    0
    Views:
    183
    Dave Angel
    Nov 21, 2012
  4. Tim Golden
    Replies:
    0
    Views:
    134
    Tim Golden
    Nov 21, 2012
  5. Prasad, Ramit
    Replies:
    0
    Views:
    167
    Prasad, Ramit
    Nov 23, 2012
Loading...

Share This Page