Sign in to follow this  
Followers 0
sambassador

End process if external drive disconnected, and restart process when reconnected

6 posts in this topic

Hi everyone, I'm new here and I hope this first post is OK. FWIW, I did search on the forum and on Google first to see if I could find an answer to my question, and I found a lot of great info but nothing that quite worked for me.

I use a program that runs in the background on my PC all the time, and relies on the connectivity of my external USB drive (which has program data stored there). My PC has been around the block a few times and occasionally the USB connections fail on boot/wake and this causes issues with the program, because it thinks all of its data has been deleted. So I'm using AutoIt to constantly monitor for the presence of this drive, and kill the program if the drive is disconnected. Then once the drive is reconnected, the script runs the program again.

After some basic testing, the script I've made (see below) seems to do the job, but the issue is that the script is constantly using 2-8% CPU, which is precious on my aging PC. I'm wondering if this is because of the DriveStatus function, and if there is a better function/method for monitoring the drive?

while 1 = 1
$status = DriveStatus ("E:")
    If $status = "INVALID" And ProcessExists("notepad.exe") Then
  ProcessClose("notepad.exe")
    ElseIf $status = "READY" And NOT ProcessExists("notepad.exe") Then
  Run("notepad.exe", "")
    EndIf
    Sleep(100)
WEnd

Many thanks for any input!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You could look into this:

It's been a little while since I last ran it though, so I can't guarantee that it still works. You could definitely get some ideas from it though.

Edit: Got it working now!

Edited by LaCastiglione

Share this post


Link to post
Share on other sites

Hi Sambassador,

I helped another forum member trying to do something similar earlier today.

$process = "notepad.exe"
$data = "ProgramData"
While 1
If _IsAttached() Then
  If Not ProcessExists($process) Then Run($process)
Else
  If ProcessExists($process) Then ProcessClose($process)
EndIf
Sleep(100)
WEnd
Func _IsAttached()
$drives = DriveGetDrive("REMOVABLE")
If Not IsArray($drives) Then
  Return False
Else
  $attached = False
  For $x = 1 to $drives[0]
   If FileExists($drives[$x] & $data) Then $attached = True
  Next
  Return $attached
EndIf
EndFunc

Set the variable on line 1 to be process that you need to have running, set the variable on line 2 to be a file that the program is looking for. On my system, this used between 0 and 1% CPU.

Share this post


Link to post
Share on other sites

u will get a good solution

Regards

PhoenixXL


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Thanks everyone - some great suggestions!

@LvlUp - looks dead simple, I'll give this one a go first.

@LaCastiglione - wow, that is quite a script! I think I can get some good ideas from this.

@PhoenixXL - I can't believe I didn't come across that thread before! Thanks for pointing it out.

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