0

I have a situation where I want to make the execution of my scripts smarter. I have a set of scripts that execute at a given time, but because sometimes the input files are not posted at the correct times the scripts run into errors and get unexpected results. so one of the solutions I was thinking of is to make the execution of the scripts dependent of each other. Here is what I mean:

  1. script 1 runs at 6 pm
  2. validates that the file is there
  3. if it's there, set a flag
  4. the flag is active so execute script 2 at 9 pm
  5. if it's NOT there, the flag is not set
  6. the flag is not set so script 2 is not executed

Right now script 1 and script 2 are set with the Task Scheduler at those times, I checked the Scheduler for those type of conditions, but didn't find anything.

5
  • ...btw, generally speaking, I would advise against using timers to schedule jobs at all. Much more reliable to trigger them by filesystem events. You want a job to run whenever a file is created in directory X? Configure that to happen, triggered by the act of a file that was opened for write being closed, and you don't need to worry about what time it is or isn't expected to show; when a new file shows up, your task takes place. Commented Oct 12, 2018 at 21:17
  • You can use that same mechanism for subsequent jobs: If job-1 creates a file when it's done, that file's creation itself can be used as the trigger for job-2, no scheduler or timer involved. See incron as an example of how one would do that on Linux. On Windows, I'm not the person to help you. Commented Oct 12, 2018 at 21:17
  • ok, I will look for an incron analogy for windows. Commented Oct 12, 2018 at 21:20
  • This item might be of some interest. superuser.com/questions/226828/… A script could be written and run every 5 minutes looking for the given file. When it is found, it should first rename the file. If the file is large, a separate trigger file should be used to indicate that the full file is completely written. Commented Oct 12, 2018 at 22:49
  • That's what I was thinking @lit. A second file to tell the script what to do. And then delete that file after the work is done. Commented Oct 13, 2018 at 5:30

2 Answers 2

2

You can set triggers in Task Scheduler, like when an event happens for basically everything you can see in eventviewer. I would suggest Write-Eventlog from the script which works on the file, and depending on the result the sched task would get triggerd.

Sign up to request clarification or add additional context in comments.

5 Comments

This is a good approach. Another option for passing "flags" between independent scripts would be creating temporary files or registry values.
@AnsgarWiechers - If scripts use registry values, will they run on Linux and Mac?
@lit Of course not. But from what I can see portability across different operating systems was not among the OP's requirements.
@AnsgarWiechers, since this is a Windows exclusive situation I think I would take the registry approach, can you specify how that would be done?
I could. But since questions about reading from and writing to the registry have been asked (and answered) multiple times already I'm going to refer you to the search box at the top of the page instead.
0

I suggest you to have single script running every N-minutes on single scheduled task via Task Scheduler.

The master script will analyze activities and have all logical conditions those determine when and which external script to run. You can also have flag files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.