How can I terminate a system command with alarm in Perl?

前端 未结 4 886
悲哀的现实
悲哀的现实 2020-12-03 12:23

I am running the below code snippet on Windows. The server starts listening continuously after reading from client. I want to terminate this command after a time period.

4条回答
  •  醉梦人生
    2020-12-03 12:50

    There are a few separate issues here.

    First, to keep the alarm from killing your script, you need to handle the ALRM signal. See the alarm documentation. You shouldn't need two scripts for this.

    Second, system doesn't capture output. You need one of the backtick variants or a pipe if you want to do that. There are answers for that on Stackoverflow already.

    Third, if alarm.pl puts anything in display.txt, you discard it in main.pl when you re-open the file in write mode. You only need to create the file in one place. When you get rid of the extra script, you won't have this problem.

    I recently had some problems with alarm and system, but switching to IPC::System::Simple fixed that.

    Good luck, :)

提交回复
热议问题