0

I need to ZIP and then send some files using FTP. The problem is that PERL executes everything and won't wait for the compression to try to send the ZIP and the script fails.

The script in pseudocode is something like this:

system("/path/to/zip/gzip myfile.txt")
system("/path/to/ftp/ftp put myfile.txt")
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
prgrm
  • 3,734
  • 14
  • 40
  • 80
  • 1
    Do not use System command. use perl modules – Jens Aug 04 '16 at 08:36
  • @Jens Any reason I should use perl modules over system command? – prgrm Aug 04 '16 at 08:37
  • 1
    If you use perl modules you program is portable – Jens Aug 04 '16 at 08:38
  • 1
    But system should wait for the command to finish anyway. Did you perhaps add an ampersand `&` within the system call? – dgw Aug 04 '16 at 08:48
  • @dgw looks like that's the problem. Let me test. – prgrm Aug 04 '16 at 08:52
  • [Please check](http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec) – ssr1012 Aug 04 '16 at 08:58
  • So you said that your code was "something like" the code you showed us. But the difference between the code you showed us and the code that you were actually using was the thing that was causing your problem. Accuracy is an important trait for a programmer to have :-) – Dave Cross Aug 04 '16 at 09:04
  • Or use `system("gzip file && ftp file.gz")` and do it all in one go, and only FTP if the gzipping is successful. – Mark Setchell Aug 04 '16 at 09:46

1 Answers1

2

system waits for the command to finish. The only way to change this behaviour is the addition of an ampersand &.

So

system( '<command> &');

will execute the command and not wait on the command to finish. To wait for the command to finish simply remove the ampersand.

system( '<command>' );
dgw
  • 13,418
  • 11
  • 56
  • 54