Windows: Scheduled Ruby script won't run

Discussion in 'Ruby' started by bdezonia@wisc.edu, Nov 7, 2007.

  1. Guest

    Hello all,

    I have looked on the web for an answer to my issue with little success
    so I am trying here. I have a ruby script that I am scheduling at
    computer startup on Windows. And apparently it never starts. If it
    starts it should write a line to a file. From the command prompt the
    script works fine but if logged in and attempting to start it from
    Scheduled Tasks it fails too with no error messages or any evidence it
    tried to start. I've checked account and password info for the task
    and it is correct. In fact I've tried to different admin acounts
    (though from a parent account). Is there something about the
    environment maybe not being loaded since its not run from a shell that
    is the problem? Any other ideas? Thanks for your insight.
    , Nov 7, 2007
    #1
    1. Advertising

  2. wrote:
    > Hello all,
    >
    > I have looked on the web for an answer to my issue with little success
    > so I am trying here. I have a ruby script that I am scheduling at
    > computer startup on Windows. And apparently it never starts. If it
    > starts it should write a line to a file. From the command prompt the
    > script works fine but if logged in and attempting to start it from
    > Scheduled Tasks it fails too with no error messages or any evidence it
    > tried to start. I've checked account and password info for the task
    > and it is correct. In fact I've tried to different admin acounts
    > (though from a parent account). Is there something about the
    > environment maybe not being loaded since its not run from a shell that
    > is the problem? Any other ideas? Thanks for your insight.
    >


    The most common problem is the path. I usually bootstrap my Ruby script
    using a .cmd file that sets the path to my Ruby interpreter and then
    calls the interpreter with the name of the script. It's always worked
    for me. Also, if for some reason, you don't trust the Windows
    scheduler, you could use PyCron - it's a cron replacement on Windows
    that works very well (specially recommended if you plan to schedule the
    script more frequently than just system startup).

    This is what I use in my scripts:

    REM Set up the environment variables for the script
    set PATH_TO_RUBY=d:\InstantRails\ruby\bin
    set RUBY=%PATH_TO_RUBY%\ruby.exe

    REM ** settings for accessing the remaining scripts **
    set PATH_TO_SCRIPTS=X:\Projects\scripts

    %RUBY% %PATH_TO_SCRIPTS%\my_script.rb

    Hope this helps.

    Cheers,
    Mohit.
    11/8/2007 | 2:47 AM.
    Mohit Sindhwani, Nov 7, 2007
    #2
    1. Advertising

  3. Mohit Sindhwani wrote:
    > The most common problem is the path. I usually bootstrap my Ruby
    > script using a .cmd file that sets the path to my Ruby interpreter and
    > then calls the interpreter with the name of the script. It's always
    > worked for me. Also, if for some reason, you don't trust the Windows
    > scheduler, you could use PyCron - it's a cron replacement on Windows
    > that works very well (specially recommended if you plan to schedule
    > the script more frequently than just system startup).
    >

    *snip*

    the other problem could be related to 'require' - if you require a file
    that is in your local directory and not in a system standard path, you
    will run into problems. You'll need to change directory to the
    directory where your scripts are before you do the require.

    Hope this helps.

    Cheers,
    Mohit.
    11/8/2007 | 3:27 AM.
    Mohit Sindhwani, Nov 7, 2007
    #3
  4. Guest

    On Nov 7, 1:27 pm, Mohit Sindhwani <> wrote:
    > Mohit Sindhwani wrote:
    > > The most common problem is the path. I usually bootstrap my Ruby
    > > script using a .cmd file that sets the path to my Ruby interpreter and
    > > then calls the interpreter with the name of the script. It's always
    > > worked for me. Also, if for some reason, you don't trust the Windows
    > > scheduler, you could use PyCron - it's a cron replacement on Windows
    > > that works very well (specially recommended if you plan to schedule
    > > the script more frequently than just system startup).

    >
    > *snip*
    >
    > the other problem could be related to 'require' - if you require a file
    > that is in your local directory and not in a system standard path, you
    > will run into problems. You'll need to change directory to the
    > directory where your scripts are before you do the require.
    >
    > Hope this helps.
    >
    > Cheers,
    > Mohit.
    > 11/8/2007 | 3:27 AM.


    Awesome idea Mohit (thanks!) but it does not work for me. The .rb
    script will launch via a .cmd file from a command prompt. Now when I
    try to start a scheduled task it at least does something. It returns
    an error code of one. My file that is written to by the first line of
    the script every time it is launched does not show any record of the
    program actually being launched. And I did not see either the .rb file
    or the ruby.exe show up in task manager (or any .cmd file either). The
    only require statement refers to 'net/smtp'. Any more ideas?
    , Nov 7, 2007
    #4
  5. wrote:
    >
    > Awesome idea Mohit (thanks!) but it does not work for me. The .rb
    > script will launch via a .cmd file from a command prompt. Now when I
    > try to start a scheduled task it at least does something. It returns
    > an error code of one. My file that is written to by the first line of
    > the script every time it is launched does not show any record of the
    > program actually being launched. And I did not see either the .rb file
    > or the ruby.exe show up in task manager (or any .cmd file either). The
    > only require statement refers to 'net/smtp'. Any more ideas?
    >


    Hmm, OK.. Can you show us what the .cmd file does - from your statement
    above, it seems that the launcher will run the .cmd file. Things don't
    work after that?

    If that's the case, the problem is either in the .cmd file or the .rb
    script. If you could show us your .cmd and/ or .rb, it may help.
    Anyway, take a look to see if there's anything in your code that assumes
    opening a file in a specific directory or something.

    If you're using SMTP at startup, are you sure that the network is up?
    It could be that it fails to connect to the network?

    Anyway, it's difficult to double guess without seeing what you're trying
    to do.

    Cheers,
    Mohit.
    11/8/2007 | 3:15 PM.
    Mohit Sindhwani, Nov 8, 2007
    #5
  6. Michael Gaunnac wrote:
    > On Nov 7, 2007 1:35 PM, <> wrote:
    >
    >
    > The attached file contains the parts of a Scheduled Task that reports
    > a restart of a Windows OS instance through email and any errors
    > associated with Terminal Services. The parts are separated by dashes.
    > This may be more than what you are looking for but it contains some
    > hard won information.
    >



    Hi, just starting to look through this. You call it with server name
    starting with \\ right?


    Cheers,
    Mohit.
    11/8/2007 | 11:45 PM.
    Mohit Sindhwani, Nov 8, 2007
    #6
  7. Michael Gaunnac wrote:
    > On Nov 7, 2007 1:35 PM, <> wrote:
    >
    >
    > The attached file contains the parts of a Scheduled Task that reports
    > a restart of a Windows OS instance through email and any errors
    > associated with Terminal Services. The parts are separated by dashes.
    > This may be more than what you are looking for but it contains some
    > hard won information.
    >

    Hi, I'm not sure and I can't tell much from the script without trying it
    out. Could you try something? In your .cmd file, can you do a chdir to
    the directory in which it's supposed to run. Do the same for the Ruby
    script also. See if that helps.

    Cheers,
    Mohit.
    11/9/2007 | 12:37 AM.
    Mohit Sindhwani, Nov 8, 2007
    #7
  8. Guest

    On Nov 8, 10:37 am, Mohit Sindhwani <> wrote:
    > Michael Gaunnac wrote:
    > > On Nov 7, 2007 1:35 PM, <> wrote:

    >
    > > The attached file contains the parts of a Scheduled Task that reports
    > > a restart of a Windows OS instance through email and any errors
    > > associated with Terminal Services. The parts are separated by dashes.
    > > This may be more than what you are looking for but it contains some
    > > hard won information.

    >
    > Hi, I'm not sure and I can't tell much from the script without trying it
    > out. Could you try something? In your .cmd file, can you do a chdir to
    > the directory in which it's supposed to run. Do the same for the Ruby
    > script also. See if that helps.
    >
    > Cheers,
    > Mohit.
    > 11/9/2007 | 12:37 AM.


    Okay, here is a test script/cmd combo that runs from the command line
    but not from a Scheduled Task under Windows. It results in an error
    code of one.

    ##### blunk.cmd file

    c:
    cd \scripts
    c:\ruby\bin\ruby.exe c:\scripts\blunk.rb

    # I have tried using the env variable idea earlier with no different
    outcome

    ##### blunk.rb file

    PATCHFILE = "c:/scripts/patchDayNotice" # also without absolute path
    tried and failed

    File.open(PATCHFILE,"a") do | file |
    file.print("BLUNK called\n")
    print "BLUNK called\n"
    end

    Any help anyone can give would be great. Is the error return code of
    one from Ruby? What error is that? I can't find any info on that.
    , Nov 8, 2007
    #8
  9. wrote:
    > Okay, here is a test script/cmd combo that runs from the command line
    > but not from a Scheduled Task under Windows. It results in an error
    > code of one.
    >
    > ##### blunk.cmd file
    >
    > c:
    > cd \scripts
    > c:\ruby\bin\ruby.exe c:\scripts\blunk.rb
    >
    > # I have tried using the env variable idea earlier with no different
    > outcome
    >
    > ##### blunk.rb file
    >
    > PATCHFILE = "c:/scripts/patchDayNotice" # also without absolute path
    > tried and failed
    >
    > File.open(PATCHFILE,"a") do | file |
    > file.print("BLUNK called\n")
    > print "BLUNK called\n"
    > end
    >
    > Any help anyone can give would be great. Is the error return code of
    > one from Ruby? What error is that? I can't find any info on that.
    >


    OK! I just tried the cmd/ rb that you sent me. Yes, it works fine from
    the command line. I also set it up as a 1-time task in Windows
    scheduler [you'll understand why I didn't set it to be something that
    runs only on bootup] and it just ran from Windows scheduler. It seems
    to work fine for me. It appends to the file.

    I did not set any advanced properties in Windows scheduler.

    Now, I'm lost as to what may be causing the problem for you.

    Cheers,
    Mohit.
    11/9/2007 | 2:55 AM.
    Mohit Sindhwani, Nov 8, 2007
    #9
  10. Guest

    On Nov 8, 12:55 pm, Mohit Sindhwani <> wrote:
    > wrote:
    > > Okay, here is a test script/cmd combo that runs from the command line
    > > but not from a Scheduled Task under Windows. It results in an error
    > > code of one.

    >
    > > ##### blunk.cmd file

    >
    > > c:
    > > cd \scripts
    > > c:\ruby\bin\ruby.exe c:\scripts\blunk.rb

    >
    > > # I have tried using the env variable idea earlier with no different
    > > outcome

    >
    > > ##### blunk.rb file

    >
    > > PATCHFILE = "c:/scripts/patchDayNotice" # also without absolute path
    > > tried and failed

    >
    > > File.open(PATCHFILE,"a") do | file |
    > > file.print("BLUNK called\n")
    > > print "BLUNK called\n"
    > > end

    >
    > > Any help anyone can give would be great. Is the error return code of
    > > one from Ruby? What error is that? I can't find any info on that.

    >
    > OK! I just tried the cmd/ rb that you sent me. Yes, it works fine from
    > the command line. I also set it up as a 1-time task in Windows
    > scheduler [you'll understand why I didn't set it to be something that
    > runs only on bootup] and it just ran from Windows scheduler. It seems
    > to work fine for me. It appends to the file.
    >
    > I did not set any advanced properties in Windows scheduler.
    >
    > Now, I'm lost as to what may be causing the problem for you.
    >
    > Cheers,
    > Mohit.
    > 11/9/2007 | 2:55 AM.


    I moved it to a different machine and it launched! Thanks Mohit!!!
    , Nov 8, 2007
    #10
  11. Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    I ran into a similar problem a while back where my code ran fine if I
    was logged into windows and using a command line, but the same code
    would not run under a batch situation. Turned out to be the security
    settings for the windows account did not allow batch running.

    Robert

    wrote:
    > On Nov 8, 12:55 pm, Mohit Sindhwani <> wrote:
    >
    >> wrote:
    >>
    >>> Okay, here is a test script/cmd combo that runs from the command line
    >>> but not from a Scheduled Task under Windows. It results in an error
    >>> code of one.
    >>>
    >>> ##### blunk.cmd file
    >>>
    >>> c:
    >>> cd \scripts
    >>> c:\ruby\bin\ruby.exe c:\scripts\blunk.rb
    >>>
    >>> # I have tried using the env variable idea earlier with no different
    >>> outcome
    >>>
    >>> ##### blunk.rb file
    >>>
    >>> PATCHFILE = "c:/scripts/patchDayNotice" # also without absolute path
    >>> tried and failed
    >>>
    >>> File.open(PATCHFILE,"a") do | file |
    >>> file.print("BLUNK called\n")
    >>> print "BLUNK called\n"
    >>> end
    >>>
    >>> Any help anyone can give would be great. Is the error return code of
    >>> one from Ruby? What error is that? I can't find any info on that.
    >>>

    >> OK! I just tried the cmd/ rb that you sent me. Yes, it works fine from
    >> the command line. I also set it up as a 1-time task in Windows
    >> scheduler [you'll understand why I didn't set it to be something that
    >> runs only on bootup] and it just ran from Windows scheduler. It seems
    >> to work fine for me. It appends to the file.
    >>
    >> I did not set any advanced properties in Windows scheduler.
    >>
    >> Now, I'm lost as to what may be causing the problem for you.
    >>
    >> Cheers,
    >> Mohit.
    >> 11/9/2007 | 2:55 AM.
    >>

    >
    > I moved it to a different machine and it launched! Thanks Mohit!!!
    >
    >
    >
    >


    --

    Robert Keller
    --------------------------------------------------
    Robert Keller, Nov 8, 2007
    #11
  12. wrote:
    >
    > I moved it to a different machine and it launched! Thanks Mohit!!!
    >


    Well, I did little more than confirming your worst fears that it wasn't
    working for you! But, I"m glad that helped.

    Cheers
    Mohit.
    Mohit Sindhwani, Nov 9, 2007
    #12
    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. Manish Kumar Arya
    Replies:
    0
    Views:
    677
    Manish Kumar Arya
    Sep 23, 2004
  2. Saurabh Agarwal
    Replies:
    0
    Views:
    586
    Saurabh Agarwal
    Sep 23, 2004
  3. kj7ny
    Replies:
    3
    Views:
    1,000
    fynali
    Jul 10, 2007
  4. Lucho
    Replies:
    5
    Views:
    3,765
    Andreas Leitgeb
    Jan 19, 2008
  5. korean_dave
    Replies:
    2
    Views:
    434
    Lawrence D'Oliveiro
    Oct 19, 2008
Loading...

Share This Page