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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

difference between system and exec ?

Status
Not open for further replies.

abruzzi

Programmer
Mar 27, 2007
17
FR
hello,

I've script a.pl who call another script b.pl in background like this,
Code:
exec ('./b.pl &');

if i use system for calling script a,
if a.pl failed b.pl failed ?

If i use exec, if a.pl failed,
b.pl continue to run;

It's that ?

Thanks
 
system() and exec() both run other programs.

system() blocks until the command you send it finishes, and then it returns and allows the code that called system() to continue. If a.pl calls system(), and a.pl closes before the system command can be completed, the system command is aborted too.

exec() replaces the calling process with the command you're wanting to call. In other words, the calling script will not continue processing commands after the exec() call, and when the exec command closes, that's the end of it, the caller is closed too.

For demonstration (Windows):

Code:
# test1.pl
system ("notepad");
print "system returned, continuing...\n";
print "exit Perl script\n";
exit(0);

# test2.pl
exec ("notepad");
print "exec returned, continuing...\n";
print "exit Perl script\n";
exit(0);

test1.pl would make Notepad run, and then when you exit out of Notepad, the console would say that "system returned", and then exit.

test2.pl would run Notepad too, but when you exit Notepad, the console program would exit too, not printing anything about exec returning.

-------------
Cuvou.com | The NEW Kirsle.net
 
i've try to use nohup,
but it's can't run good
i've make this :
Code:
system ('nohup ./b.pl &');
 
i've try this
but it's not run
when the father dead , the child (finded.pl) died;
Code:
my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd) or 
die "Database connection not made: $DBI::errstr";

my $req ="select * from folders where tmp='SL'";

my $sth=$dbh->prepare($req);
$sth->execute();

while (my ($a,$b,$c,$d) = $sth->fetchrow_array()) 
{
$times = $d;



my $doss = $a;
my $appli=$c;


system ('nohup perl ./finded.pl -d '.$doss.' -c '.$appli.' -b '.$b.' &');

}

$sth -> finish;

$dbh -> disconnect;
 
fork it off!

Of course that has it's own other set of issue... but you don't care about what it's doing.. fork. If you are doing all of this on windows (some of your stuff looks like windows stuff like notepad so I'm guessing) you can start all your commands with "start"
 
Fork it off then.. if you can't get any other way to work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top