czchlong
czchlong

Reputation: 2574

Perl script in loop ends after one run

I am running a Perl script in a loop in a shell script:

  while [ $currentDate -le $toDate ]; do
    // code here...
    exec /users/my_user/script_name $currentDate
    // code here...
  done

I have confirmed the while-loop loops. However, after one run of the Perl script, the while-loop ends.

Could someone please shed some light on this?

Upvotes: 3

Views: 821

Answers (3)

Joe W
Joe W

Reputation: 1872

The issue you are having is caused by

exec /users/my_user/script_name $currentDate

what the exec causes is for the perl script that you are calling replaces the current program with the new one and keeps the current PID.

If you remove the exec from the line it will allow the program to spawn properly and keep the shell running as you expect.

Upvotes: 1

nandhp
nandhp

Reputation: 4817

You're using exec. exec replaces the shell process with the new program. You probably want to just remove the exec keyword.

exec [-cl] [-a name] [command [arguments ...]] [redirection ...]

Replace the shell with the given command.

Execute COMMAND, replacing this shell with the specified program. ARGUMENTS become the arguments to COMMAND.

Upvotes: 11

Sean Bright
Sean Bright

Reputation: 120634

Change:

exec /users/my_user/script_name $currentDate

To:

/users/my_user/script_name $currentDate

Upvotes: 1

Related Questions