Audacity and Windows Task Scheduler

In the Windows Task Scheduler, I can run Audacity at 3:48 and then at 3:50, automatically starting Python recording.
However, sometimes there is an error in the task schedule. Or the Audacity.ece application freezes, it is usually described in the schedule as error 0xFFFFF. Then the automatic recording doesn’t start either and I have to manually stop the program and restart manually. Today, for example, it supposedly started automatically, started recording, but after a while it also crashed and recording stopped, I only noticed it after a few hours.
Sometimes it also happened that the application opened automatically without errors, but the 0x1 error caused Python to be automatically recorded.
Is the problem with Audacity or with my system? Does anyone know how to deal with this topic?
I will add that often everything starts correctly, these errors occur from time to time.

Hope the translation is quite correct and understandable :slight_smile:

Have I got this right?

  1. You are launching Audacity from the Windows Task Scheduler
  2. Then you run a Python command from the Windows Task Scheduler to send a command to Audacity via mod-script-pipe to start recording?
  3. Sometimes this works but sometimes it fails.

What are the exact commands?
How do you ensure that the Python command comes after Audacity has fully started?

  1. yes
  2. yes
  3. yes

The python.py file is written like this, I am pasting it below. It usually works. It happens, however, that it will not start, for which, I do not know.
And the crash of the application itself at startup is a different problem.

import of os
import sys


if sys.platform == 'win32':
    print ("pipe-test.py, running on windows")
    TONAME = '\\\\. \\ pipe \\ ToSrvPipe'
    FROMNAME = '\\\\. \\ pipe \\ FromSrvPipe'
    EOL = '\ r \ n \ 0'
else:
    print ("pipe-test.py, running on linux or mac")
    TONAME = '/tmp/audacity_script_pipe.to.' + str (os.getuid ())
    FROMNAME = '/tmp/audacity_script_pipe.from.' + str (os.getuid ())
    EOL = '\ n'

print ("Write to \" "+ TONAME +" \ "")
if not os.path.exists (TONAME):
    print ("..does not exist. Ensure Audacity is running with mod-script-pipe.")
    sys.exit ()

print ("Read from \" "+ FROMNAME +" \ "")
if not os.path.exists (FROMNAME):
    print ("..does not exist. Ensure Audacity is running with mod-script-pipe.")
    sys.exit ()

print ("- Both pipes exist. Good.")

TOFILE = open (TONAME, 'w')
print ("- File to write to has been opened")
FROMFILE = open (FROMNAME, 'rt')
print ("- File to read from has now been opened too \ r \ n")


def send_command (command):
    "" "Send a single command." ""
    print ("Send: >>> \ n" + command)
    TOFILE.write (command + EOL)
    TOFILE.flush ()

def get_response ():
    "" "Return the command response." ""
    result = ''
    line = ''
    while line! = '\ n':
        result + = line
        line = FROMFILE.readline ()
        #print ("I read line: [" + line + "]")
    return result

def do_command (command):
    "" "Send one command, and return the response." ""
    send_command (command)
    response = get_response ()
    print ("Rcvd: <<< \ n" + response)
    return response

def run ():
    do_command ("Record1stChoice:")
run()

Perhaps a problem with copy / paste to the forum, but there appear to be some errors in that script:

print ("Write to \" "+ TONAME +" \ "")

should probably be

print ("Write to \" "+ TONAME +" \"")



print ("Read from \" "+ FROMNAME +" \ "")

should probably be

print ("Read from \" "+ FROMNAME +" \"")

(though both of the above could simply be commented out)


Your code is not exception safe. If anything fails, the script just carries on, ignoring the error, until it crashes.

At the very least you should check that both pipes exist, and check that Audacity is running before trying to send the command. Try to catch errors and exit with a meaningful error on failure. The classic Python way to handle errors is “Try / Except”.
See:
https://www.w3schools.com/python/python_try_except.asp
and
https://realpython.com/python-exceptions/

So you have two minutes; have you considered doubling the time ?

I can, but I doubt it will change anything. With normal operation, the program starts in a second and is basically ready to run. And here are 2 minutes from starting the program to the start of recording. I have never encountered a crash of Audacity when I run it manually from the desktop.

What steve said. :smiley:

This is an error that i’m getting after script is opened by task scheduler. It seems like it cannot open the pipe for some reason.

    FROMFILE = open(FROMNAME, 'rt')
FileNotFoundError: [Errno 2] No such file or directory: '\\\\.\\pipe\\FromSrvPipe'

but when I try manually open the script after the first error is appearing I get this one:

Write to  "\\.\pipe\ToSrvPipe"
 ..does not exist.  Ensure Audacity is running with mod-script-pipe.

It happens time to time but mostly works as expected.

Perhaps you need to put in a short delay before

FROMFILE = open(FROMNAME, 'rt')



That suggests some sort of timing issue.

Can it be done somehow from Audacity? I would like to add that sometimes after unsuccessful start of recording, I run the script again and recording starts without any problems. But sometimes Audacity freezes.
I’m using version 2.3.3 - but I don’t know if it matters in this situation.

Perhaps worth trying the current 2.4.2 version Audacity ® | Download for Windows

I will try. It is a pity that Audacity does not have such an additional option to start recording when the program is turned on :slight_smile: But I know that my case is quite specific …