Sign in to follow this  
Followers 0
Joshjosh

Restart a service if a file timestamp changes?

9 posts in this topic

Hey guys,

A have a service running called "music" which runs a command line mp3 player that points to a playlist. Now what I want to do is restart this service if the playlist file changes (i.e. I over-write it with a newer one) and therefore it picks up the new songs.

Any help with this would be much appreciated!!

Share this post


Link to post
Share on other sites



Hey guys,

A have a service running called "music" which runs a command line mp3 player that points to a playlist. Now what I want to do is restart this service if the playlist file changes (i.e. I over-write it with a newer one) and therefore it picks up the new songs.

Any help with this would be much appreciated!!

Both servicecontrol.au3 and service.au3 are taken from here http://www.autoitscript.com/forum/index.php?showtopic=42443

#include <ServiceControl.au3>
#include <Service.au3>

Global $file_to_monitor = "C:\Version.txt"
Global $on_start_value = CheckForFileUpdate($file_to_monitor)

While 1 
    Sleep(1000)
    $return_date = CheckForFileUpdate($file_to_monitor)
    If $return_date <> $on_start_value Then
        _RestartServices() ; restarts service 'Music', could have some error checking here
        $on_start_value = $return_date ; resets the on_start_value to newest date / time value
    EndIf
WEnd


Func CheckForFileUpdate($file_to_check)
    $file_time = FileGetTime($file_to_check, 1)
    If Not @error Then
        $yyyymd = $file_time[0] & "/" & $file_time[1] & "/" & $file_time[2]
        $hhmmss = $file_time[3] & ":" & $file_time[4] & ":" & $file_time[5]
        Return $yyyymd & " " & $hhmmss
    EndIf
EndFunc   ;==>CheckForFileUpdate
Func _RestartServices()
    Local $time1, $time2
    If _ServiceExists ("", "Music") = 1 Then
        If _ServGetState("Music") = "Running"  Then
            _StopService ("", "Music")
        EndIf
        $time1 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Stopped"  Then ExitLoop
        Until TimerDiff($time1) > 20000
        _StartService("", "Music")
        $time2 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Running"  Then Return "1" 
        Until TimerDiff($time2) > 20000; check timeout
    EndIf
    Return "0" 
EndFunc   ;==>_RestartServices

Work from that <_<

service.au3

servicecontrol.au3


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

thanks for that madboy, its much appreciated.

I have compiled it and attempted to run but unfortuantely nothing happens. The service is present along with the version file but no action. Any ideas?

Share this post


Link to post
Share on other sites

thanks for that madboy, its much appreciated.

I have compiled it and attempted to run but unfortuantely nothing happens. The service is present along with the version file but no action. Any ideas?

Have you changed

Global $file_to_monitor = "C:\Version.txt" to your file? Also keep in mind that the code i gave you when started it doesn't restart the service at all. It just gathers information. Just the next time the file is changed it will restart service.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Have you changed

Global $file_to_monitor = "C:\Version.txt" to your file? Also keep in mind that the code i gave you when started it doesn't restart the service at all. It just gathers information. Just the next time the file is changed it will restart service.

Sorry it's not working because it's getting the date of creation and not the date of modification. I'll try to fix.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Sorry it's not working because it's getting the date of creation and not the date of modification. I'll try to fix.

#include <ServiceControl.au3>
#include <Service.au3>

Global $file_to_monitor = "C:\Version.txt"
Global $on_start_value = CheckForFileUpdate($file_to_monitor)

While 1 
    Sleep(1000)
    $return_date = CheckForFileUpdate($file_to_monitor)
    ;ConsoleWrite(@CRLF & $return_date & " - " & $on_start_value)
    If $return_date <> $on_start_value Then
        _RestartServices() ; restarts service 'Music', could have some error checking here
        $on_start_value = $return_date ; resets the on_start_value to newest date / time value
    EndIf
WEnd


Func CheckForFileUpdate($file_to_check)
    $file_time = FileGetTime($file_to_check, 0)
    If Not @error Then
        $yyyymd = $file_time[0] & "/" & $file_time[1] & "/" & $file_time[2]
        $hhmmss = $file_time[3] & ":" & $file_time[4] & ":" & $file_time[5]
        Return $yyyymd & " " & $hhmmss
    EndIf
    Return 0
EndFunc   ;==>CheckForFileUpdate
Func _RestartServices()
    Local $time1, $time2
    If _ServiceExists ("", "Music") = 1 Then
        If _ServGetState("Music") = "Running"  Then
            _StopService ("", "Music")
        EndIf
        $time1 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Stopped"  Then ExitLoop
        Until TimerDiff($time1) > 20000
        _StartService("", "Music")
        $time2 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Running"  Then Return "1" 
        Until TimerDiff($time2) > 20000; check timeout
    EndIf
    Return "0" 
EndFunc   ;==>_RestartServices

That should fix it.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Hey MadBoy,

I appreciated all your help and dont mean to be a pain but I still am not having much luck. I have created the Version.txt file under C:\ and started the compiled exe from the code above. When i make changes to the version.txt file nothing happens. I do noticed that the AutoScript icon flashes between the normal icon and red cross and when you right click on it it says its paused. Any ideas? I assume this means the script doesnt run properly. Is it case sensitive at all?

Any help would be much appreciated.

Share this post


Link to post
Share on other sites

scratch that, it is case sensitive, I have the service install as "music" and not "Music". Reinstalled service as Music and it works a treat. Thankyou so much for your help, it is greatly appreciated.

Share this post


Link to post
Share on other sites

scratch that, it is case sensitive, I have the service install as "music" and not "Music". Reinstalled service as Music and it works a treat. Thankyou so much for your help, it is greatly appreciated.

You should be aware that clicking on the icon pauses the script. So if you got near the autoit icon you have paused it by accident <_< That's why it wasn't working aswell.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0