Windows Scheduled Task and Console Application - windows

I'm creating a simple job that I'd like to run every 60 seconds (1 minute). Instead of approaching this using a Windows Service Installer program, I'd like to create a Console App that is run through Windows Scheduler.
What will happen if a job kicks off and it takes more than 60 seconds to process? Will a second instance of a job kick off again? I'd like Windows to manage the job in a manner such that a second instance of a job won't kick off until the first one is complete.
Does this make sense? Will the Windows Task Scheduler use this approach by default?
Thanks all.

Yes, Windows will start a new instance by default; however, you can control that behavior. In Control Panel, go to Task Scheduler. Find any time-scheduled task and right click/Properties, then choose the Settings tab. At the bottom, you'll see an option named "If the task is already running, then the following rule applies." You can choose to not start a new instance, run a new instance in parallel, queue a new instance, or stop the existing instance. You want the first choice. The Windows Task Scheduler API provides the same functionality.

Related

UWP apps running background tasks when the app is closed

I want to run a background task for every 10 mins.As the UWP app will not always be opened.Is there a way to run a background task even when the app is closed(Not Running State)?
If the answer is NO,
Is there any another way other than Windows Services?
You can create BackgroundTask which run at the most every 15 minutes not less than that in UWP apps. for more details you can check this source.
Is there a way to run a background task even when the app is
closed(Not Running State)?
According to this document:
You can use background tasks to provide functionality when your app is
suspended or not running.
The 'not running' state in UWP is described in more detail here. It reads:
An app could be in this state because it hasn't been launched since
the last time the user rebooted or logged in. It can also be in this
state if it was running but then crashed, or because the user closed
it earlier.
This implies that the app simply needs to run once after installation to be able to successfully register a background task, which can then run even after your app is closed.
I want to run a background task for every 10 mins
Not sure I'm interpreting this correctly but I assume you mean you want to run some bit of code every 10 mins while your app is not running. Well there are two way that I can think of to achieve this.
Method 1:
While background tasks are meant to be very short-lived tasks, they may even be made to run indefinitely if:
extendedBackgroundTaskTime capability is added as a restricted
capability in your app's manifest file
Using this technique along with a simple timer mechanism would achieve the desired result.
Method 2 (more complicated but keeps background tasks short-lived):
Setup a DatagramSocket to a remote server which sends some data every 10 mins and register your background task with a SocketActivityTrigger.

How can I run an alert program when a Windows Service fails to start?

I have a Windows service whose start-up is set to 'Automatic', so it starts whenever the system boots up.
But this jobs sometimes did not start due to unknown reasons. I want to setup an alert program (.bat) to run whenever the service fails to start.
I tried using Recovery Mode options in service property, but it is not working. I think it is for jobs crashing with errors.
You can create a script that lists the service of yours and see whether it is running (using the sc command). Then you might to put that script to Startup menu. Should the script run before the service is started, you would need to schedule the script to be running periodically (to give the service some time to be started) using either of schtasks, at, or back in the nt4 days there was (still functional I presume) nice gui version of at in nt 4 resource kit called winat, if you have access to it.

Run console application automatically in windows

Is it possible to automatically run a console application on a particular time in a day.
With out set up any sheduler !
Not without any scheduler; something needs to invoke it.
Alternatively, you can write a separate app that runs on startup and starts your app for you at the desired time. This would mean that this background app would always be open.
Using the scheduler would be much easier.
If with "set up any scheduler" you refer to not installing something to do the schedule, there is no need to do that. Windows include it own scheduler.

Windows Task Scheduler will run app only once

So my situation is that I am running an app on the Windows Task Scheduler. This app is run once a day at 1pm. the app does some queries and transfers data to an FTP site. All that is working great except on the weekends when i am not here the app is run and the GUI is still displayed for me to review. This seems to make it stop running on the scheduler until I shut down the app. So on Saturday it will run and the app will remain displayed for me to review when I get back on Monday. but on Sunday when the scheduler attempts to run it again it will fail because the app has not been closed down.
First let me confirm that this is how the Task Scheduler is supposed to work. Second, what are my alternatives for scheduling to run every day and keep the GUI displayed so that I can review. The app can run multiple times as each session does not interfere with the other sessions. So if I'm gone for a week on vacation I would expect that when i get back that 7 instances of the app have been run and are waiting for my review.
Thanks
AGP
Your best bet is to eliminate the UI and log messages to the Event Log or a log file. The UI could be spawned from the CLI as a separate process if you prefer, but it should be done so in as its own non-child process.
Alternatively, you could run a batch file instead of the process directly. In the batch file, invoke "START path_to_exe" instead of the EXE. That will cause the batch file to "finish" instantly, and the exe to be run in its own process. This is not a good long term solution, but will give you a temporary solution to your immediate problem.
This is the default behavior of the Scheduled Task system, as it doesn't know that the job is complete until the application actually exits. Therefore, if your application is still open after 24 hours, the next run will simply be skipped because the current run is "still going" as far as the scheduler is concerned.
Personally I would re-visit the way that you handle your job process, as your are setting up a scenario that will be hard to manage long term.
I recommend writing to a log file instead of displaying a UI for any output and/or errors. This way, the application can write, then exit, and you can review the log at your convenience. This is a very common solution for automated processes.

Possible to only initiate scheduled task if shortcut/button clicked?

I've created 2 tasks in Task Scheduler on my Vista PC start uTorrent at 2am then close uTorrent (and shutdown PC) at 7am. However i'd like to only like this task to run if I've clicked a shortcut - ideally show something in the tray as well if possible. But not sure how?
Thanks in advance!
I managed to to something like this myself in windows 7, should be the same in Vista.
To do what you want you could create a scheduled task that starts utorrent, another to close it later and another to then shut down the pc, all timed to run every day at 2am. You would then create a task that runs a batch file to delete the other tasks every time you boot up.
You will have exported the first three tasks as XML files just after you created them and saved them somewhere.
The key is that you then create a batch file that, upon double clicking will create the scheduled tasks by importing the XML files. This will cause the tasks to run as before and shut down your pc afterwards. Next time you boot up your machine the tasks are deleted again by the first batch file.
Read up on batch files (simple command propt commands in a file that run when clicked) and the command-line interface for scheduled tasks.
Type schtasks /? at the command promt for more info.
Hope that makes sense.

Resources