Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a php script to check if there are any new files in a folder and, if any new files exist, upload them to a server. These files can be quite large. I want to run this script frequently-let's say every 5 min-as a scheduled task so that the files get moved to the server as soon as possible. However, once the script is already attempting to upload a file, I do not want it to be run again, as I am afraid the second instance will overwrite the file that is already being uploaded to the server.

How can I run the script as a scheduled task unless the script is already running?

share|improve this question
up vote 14 down vote accepted

Assuming you're just setting the task to "Repeat" in the XP "Scheduled Tasks" system, no further action on your part is needed. Scheduled Tasks won't "Repeat" a task if it's already running.

If you want to override that default, you can check the box "If the task is still running, stop it at this time" to cause the task scheduler to kill the last instance before starting a new one (though it sounds like you probably don't want that).

share|improve this answer
Thanks for the quick and correct response. – Oren Hizkiya Jul 14 '09 at 15:41

The way I have done this is to run the task from a batch file that starts once a day shortly after midnight and runs until next midnight. The advantag of this is that because it's a single script you cannot get a second instance running. I suppose a disadvantage might be that you can predict exactly how often the script runs because it would wait a set time after each run. Anyhow if you're interested my script looks like:

rem *** Get the day number. When the day number changes that signals
rem *** the script to exit.

for /f "tokens=1" %%i in ('date /t') do set THEDATE=%%i

rem *** Open the log file

echo Starting Whatever >%LOGFILE%
date /t >>%LOGFILE%
echo . >>%LOGFILE%

rem *** Start the monitoring loop


rem *** Run your script

rem Run the script here

rem *** Pause for 300 seconds

sleep 300

rem *** Check the day number. If it is still the same loop back to the
rem *** start of the monitoring loop.

for /f "tokens=1" %%i in ('date /t') do set THEDATE=%%i
if %THEDATE:~0,2% == %THEDAY% goto start


share|improve this answer

In the windows task scheduler there is a checkbox (or options) for what to do if the task is already running when the task starts again. You can set it not to start or run in parallel

share|improve this answer
Where is that setting in the XP task scheduler? I'm not finding any way to get the XP scheduler to execute the same task multiple times in parallel. – Evan Anderson Jul 14 '09 at 15:17
Vista. I noticed the windows-xp tag after I entered my answer. – Kevin Kuphal Jul 14 '09 at 15:29

The usual way is to test for some global thingy (often a specific empty file) when the task starts. If it exists, the task is already running. If not, set the global thingy, do your stuff, unset the global thingy when the task is done.

This has problems if the task fails without unsetting the global thingy. You may want to always unset it in the computer startup routine since the task can't be running if you've restarted. Better is to find a global thingy that goes away automatically when the task ends.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.