2
[user@notebook ~] time perl -e 'alarm shift @ARGV; exec @ARGV' 1 sleep 2
Alarm clock

real    0m1.004s
user    0m0.004s
sys 0m0.000s
[user@notebook ~] 

My question: how can I get rid of that "Alarm clock" message?? I just want to terminate a process after given time with a perl oneliner..

2 Answers 2

4

That message is from bash, not Perl.

One way is to run it in a subshell:

$ ( time perl -e 'alarm shift @ARGV; exec @ARGV' 1 sleep 2 )

real    0m1.004s
user    0m0.003s
sys 0m0.000s
1
  • Seems like it's the time command and subshell command that do this - I had to suppress stderr output as well to avoid the time output. Ended up quite messy compared to GNU timeout from coreutils.
    – RichVel
    Nov 16, 2017 at 15:51
1

If you want to run a command with a timeout, you may want to try the command timeout and avoid reinventing the wheel:

timeout 2 sleep 3

If you get an exit code of 124, the time has run out. Otherwise, you'll get the exit code of the executed command.

It's part of the GNU coreutils package, you're likely to already have it.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.