Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Deleting a running script

by Bagarre (Sexton)
on Sep 16, 2003 at 15:58 UTC ( [id://291858]=perlquestion: print w/replies, xml ) Need Help??

Bagarre has asked for the wisdom of the Perl Monks concerning the following question:

This may not be a PERL question per se' but, here goes. I need to figure out a way to have a program delete itself. The perl script is wrapped with perl2exe and the last thing I need it to do is delete itself. Thanks!

Replies are listed 'Best First'.
Re: Deleting a running script
by waswas-fng (Curate) on Sep 16, 2003 at 16:13 UTC
    It really is not a perl question, but it is possible. On unix you may unlink the running program's file (on most versions of unix at least) and the file will be deleted (it still really exists until the proccess ends. I do not think you can do it the same way on windows platforms -- you may have to release control (exec) to another program that removes the exe that you were running. for windows that may mean execing cmd with the delete command in the exec string. one problem here is that if your exec fails for some reason you have no chance to recover...

    -Waswas
      The killer is, it's a Windoz platform. Unlink would work like a charm if I was in a 'nix world. Windows puts a lock on the file and I can't even 'manualy' delete it from the command prompt while she's running :( I tried scheduling a job to do it for me just after the program finishes but, that opened another can of worms. I was hoping to take care of it from within the program itself.
        Bagarre,
        waswas-fng gave you the answer. You need to have the last few lines of your program create a batch file. The batch file looks something like:
        del script.exe del batch.bat
        The final line of code would be:
        exec ("batch.bat");
        Why is it important for the executable to delete itself anyway?

        L~R

        There is a way to do this by setting a value in the registry to run the program on the next reboot and then forcing a reboot on the system.

        I can't offer any additional guidance since I am certainly no Windows Programming Guru....except to say that I know this can be done because I have seen it in commercial installer programs.

        I found this artcle on forcing a reboot on http://www.devx.com/ which is not a perl solution but perhaps there is a way to do this with one of the Win32 modules on CPAN

Re: Deleting a running script
by graff (Chancellor) on Sep 17, 2003 at 05:02 UTC
    I'm just grasping as straws here (if I were using windows myself, I guess I'd be gasping at flaws...) -- anyway, I wonder if you can figure a way to have your script manipulate the windows "task scheduler" so that it will execute some windowless process to delete your script file at, say, a minute or two after the current time when the script is about to exit. (Needless/heedless to say, "cron" on *nix would make this sort of approach very easy.) Good luck.
Re: Deleting a running script
by waswas-fng (Curate) on Sep 18, 2003 at 17:29 UTC
    On windows you may execute an at job (see the at command) to run 5 minutes after the process completes -- do this right before your program would normally exit and delete the entire directory your malware is running from. This will resolve the dll left behind issue and is built into nt -> xp

    -Waswas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://291858]
Approved by tcf22
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found