Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Problem with subprocess.call and windows schtasks

81 views
Skip to first unread message

Tim Golden

unread,
Nov 18, 2012, 11:19:19 AM11/18/12
to pytho...@python.org
On 18/11/2012 13:48, Tom Borkin wrote:
> 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.

Generally, with subprocess call:

* Pass each element in the command line as a separate element in the
list: ['SchTasks', '/Create', '/SC', 'ONCE', ... &c.]

* Don't use shell=True unless you're running a command which is internal
to cmd.exe (such as dir, copy, etc.). If you're running anything which
exists as its own .exe, you shouldn't be using shell=True.

TJG

Chris Rebert

unread,
Nov 18, 2012, 11:39:27 AM11/18/12
to Tom Borkin, pytho...@python.org
On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin <bork...@gmail.com> 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

Dave Angel

unread,
Nov 20, 2012, 8:28:24 PM11/20/12
to Tom Borkin, pytho...@python.org
On 11/20/2012 06:41 PM, Tom Borkin wrote:

(Please don't top-post. Now we lose all the context)
> Using shlex, I now have this:
> #!\Python27\python
> import os, subprocess
> path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
> "htdocs", "ccc", "run_alert.py")
> #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', path, '/ST', '23:50'])
> subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', 'run_alert.py', '/ST', '23:50'])
> Both of the above commands throw the same error:
> ERROR: The filename, directory name or volume label syntax is incorrect.

I don't use Windows, but doesn't a Windows program usually have an .exe
extension? So why would you expect it to find SchTasks ? Adding
extensions is a shell feature, and you're not using the shell.

Also, you should take a look at the value "path". On Linux, it shows up as:

C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py

It'll be different under Windows, but probably still wrong.


--

DaveA

Tim Golden

unread,
Nov 21, 2012, 5:01:33 AM11/21/12
to pytho...@python.org
On 20/11/2012 23:41, Tom Borkin wrote:
> Using shlex, I now have this:
> #!\Python27\python
> import os, subprocess
> path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
> "htdocs", "ccc", "run_alert.py")
> #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', path, '/ST', '23:50'])
> subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', 'run_alert.py', '/ST', '23:50'])
> Both of the above commands throw the same error:
> ERROR: The filename, directory name or volume label syntax is incorrect.

The following works for me:

import subprocess
path = r"C:\Program Files\Apache Group\Apache2\htdocs\ccc\run_alert.py"
subprocess.call([
'SchTasks', '/Create',
'/SC', 'ONCE',
'/TN', 'test',
'/TR', path,
'/ST', '23:50'
])

Things to note:

* I haven't added extra quoting to any of the items in the command list
which is subprocess.call's first parameter. The point about using the
list (as opposed to passing an entire string which you can also do) is
that the subprocess module can quote things as needed. If you've already
added quotes, you'll get double-quoting which you almost certainly don't
want.

* (Obviously) I've formatted the subprocess.call as I have for clarity,
especially via email. It's just a list.

TJG

Prasad, Ramit

unread,
Nov 23, 2012, 5:32:48 PM11/23/12
to Tom Borkin, pytho...@python.org
Dave Angel wrote:
>
> On 11/20/2012 06:41 PM, Tom Borkin wrote:
>
> (Please don't top-post. Now we lose all the context)
> > Using shlex, I now have this:
> > #!\Python27\python
> > import os, subprocess
> > path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
> > "htdocs", "ccc", "run_alert.py")
> > #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> > '/TR', path, '/ST', '23:50'])
> > subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> > '/TR', 'run_alert.py', '/ST', '23:50'])
> > Both of the above commands throw the same error:
> > ERROR: The filename, directory name or volume label syntax is incorrect.
>
> I don't use Windows, but doesn't a Windows program usually have an .exe
> extension? So why would you expect it to find SchTasks ? Adding
> extensions is a shell feature, and you're not using the shell.
>
> Also, you should take a look at the value "path". On Linux, it shows up as:
>
> C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py
>
> It'll be different under Windows, but probably still wrong.

Windows 7 + Python 2.6
>>> os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
... "htdocs", "ccc", "run_alert.py")
'C:\\Program Files\\Apache Group\\Apache2\\htdocs\\ccc\\run_alert.py'


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
0 new messages