2

I have a simple .bat that runs a PHP script at the same time every day.

@echo ON
php C:\path\to\script.php argument
pause

It runs fine when I double-click the .bat file. And I set up a scheduled task to run it everyday, but it runs in the background. No Command line window anywhere. I can see that it is indeed running.

I would really like to be able to monitor this job as it runs.

How can I force this to run in a command-line window in the foreground?

Windows 7 environment.

3 Answers 3

3

When you schedule the task, if you set it to run as the user that will be logged in at the time, It should run in the foreground. There is also a "Hidden" checkbox under the general tab. Ensure that is unchecked.

3

I actually found this answer over on StackOverflow.

In the Scheduled Task properties, under the 'Actions' tab, choose your action and click edit.

Under the program/script field, change this to cmd.

Under the add arguments field, change this to `/k "C:\path\to\job.bat".

This worked for me.

0

The schtasks command is quite particular in how it deals with users and background/foreground. I much prefer creating and managing scheduled tasks from the command line (much less hassle than the GUI and I can adjust things exactly to my liking very quickly this way), but foreground/background is a specific issue. To show how it works with examples:

The following task will run every 30 minutes (but only in the background! The use of /ru and /rp forces this to happen):

schtasks /create /sc minute /mo 30 /tn "Notes" /tr notepad.exe /ru /rp

However, if you run the following command, it will run in the foreground with your credentials (so if you want to monitor the job, you will be able to do that as follows):

schtasks /create /sc minute /mo 30 /tn "Notes" /tr notepad.exe

This is quite odd the first time I saw it, but once you know the rule, it's simple. As you say, oddly in the GUI, you have to set the username/password as the current user for it to work in the foreground, which seems opposite.

Some more examples:

schtasks /create /sc minute /mo 30 /tn "Notes" /tr "php C:\path\to\script.php argument"

schtasks /create /sc minute /mo 30 /tn "Notes" /tr "start /min C:\Users\John\MyPHPscript.bat"

then put into the MyPHPscript.bat: php C:\path\to\script.php argument

To properly view the specific task (showing also the command line used):

schtasks /query /tn "Notes" /v /fo list

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .