Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to let user abort system() command

Status
Not open for further replies.

redsss

Programmer
Mar 17, 2004
72
US
I have a command that will take a couple minutes to execute.

I want to do something like this:
Code:
system(command) until (done or user_aborted_with_keypress)
quit if done;
do something special if user hit 'N'
Any ideas on the best way to do this?
 
print "User, are you sure you wait to wait\n";
print "Type Y for yes or N for no\n";
chomp ($input = <STDIN>);
if ($input eq N){&exit) else ($input eq Y){&run_command};

sub run_command {
system(command);
}

sub exit {
print "Sorry you are so impatient\n";
}









HTH - Stiddy. That should get you on the right track.
 
Do you want to give them a chance to opt out of executing the command (Stiddy's method) or run it in a separate thread and allow them to cancel execution half way through when they get fed up with waiting (obviously CTRL-C is a simple if rather inelegant option here).
 
Obviously I would want to start executing the process but allow the user to abort the thread if it takes too long, without aborting the parent process.

I figured out I can just spawn a command in the background with system("command &") and then allow them to abort by killing the PID within the script. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top