Audacity and Windows Task Scheduler

Help for Audacity on Windows.
Forum rules
ImageThis forum is for Audacity on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at https://www.audacityteam.org/download/.
The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.
sebalorek
Posts: 20
Joined: Mon Apr 13, 2020 6:22 pm
Operating System: Windows 7

Audacity and Windows Task Scheduler

Post by sebalorek » Mon Dec 21, 2020 11:03 am

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 :)

steve
Site Admin
Posts: 80752
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Audacity and Windows Task Scheduler

Post by steve » Mon Dec 21, 2020 12:29 pm

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?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

sebalorek
Posts: 20
Joined: Mon Apr 13, 2020 6:22 pm
Operating System: Windows 7

Re: Audacity and Windows Task Scheduler

Post by sebalorek » Mon Dec 21, 2020 12:40 pm

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.

Code: Select all

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()
Last edited by steve on Mon Dec 21, 2020 1:13 pm, edited 1 time in total.
Reason: code tags added

steve
Site Admin
Posts: 80752
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Audacity and Windows Task Scheduler

Post by steve » Mon Dec 21, 2020 1:28 pm

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

Code: Select all

print ("Write to \" "+ TONAME +" \ "")
should probably be

Code: Select all

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

Code: Select all

print ("Read from \" "+ FROMNAME +" \ "")
should probably be

Code: Select all

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/
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

jademan
Forum Crew
Posts: 1179
Joined: Fri Jul 17, 2009 10:11 pm
Operating System: Please select

Re: Audacity and Windows Task Scheduler

Post by jademan » Mon Dec 21, 2020 2:15 pm

sebalorek wrote:
Mon Dec 21, 2020 11:03 am
In the Windows Task Scheduler, I can run Audacity at 3:48 and then at 3:50...
So you have two minutes; have you considered doubling the time ?

sebalorek
Posts: 20
Joined: Mon Apr 13, 2020 6:22 pm
Operating System: Windows 7

Re: Audacity and Windows Task Scheduler

Post by sebalorek » Mon Dec 21, 2020 2:35 pm

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.

jademan
Forum Crew
Posts: 1179
Joined: Fri Jul 17, 2009 10:11 pm
Operating System: Please select

Re: Audacity and Windows Task Scheduler

Post by jademan » Mon Dec 21, 2020 2:58 pm

steve wrote:
Mon Dec 21, 2020 1:28 pm
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/
What steve said. :D

sebalorek
Posts: 20
Joined: Mon Apr 13, 2020 6:22 pm
Operating System: Windows 7

Re: Audacity and Windows Task Scheduler

Post by sebalorek » Tue Dec 29, 2020 5:51 pm

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.

Code: Select all

    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:

Code: Select all

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.

steve
Site Admin
Posts: 80752
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Audacity and Windows Task Scheduler

Post by steve » Tue Dec 29, 2020 7:10 pm

Perhaps you need to put in a short delay before

Code: Select all

FROMFILE = open(FROMNAME, 'rt')
sebalorek wrote:
Tue Dec 29, 2020 5:51 pm
It happens time to time but mostly works as expected.
That suggests some sort of timing issue.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

sebalorek
Posts: 20
Joined: Mon Apr 13, 2020 6:22 pm
Operating System: Windows 7

Re: Audacity and Windows Task Scheduler

Post by sebalorek » Tue Dec 29, 2020 7:43 pm

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.

Post Reply