Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:03:19+00:00 2026-05-13T22:03:19+00:00

In Linux I would like to run a program but only for a limited

  • 0

In Linux I would like to run a program but only for a limited time, like 1 second. If the program exceeds this running time I would like to kill the process and show an error message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:03:19+00:00Added an answer on May 13, 2026 at 10:03 pm

    StackOverflow won’t allow me to delete my answer since it’s the accepted one. It’s garnering down-votes since it’s at the top of the list with a better solution below it. If you’re on a GNU system, please use timeout instead as suggested by @wRAR. So in the hopes that you’ll stop down-voting, here’s how it works:

    timeout 1s ./myProgram 
    

    You can use s, m, h or d for seconds (the default if omitted), minutes, hours or days. A nifty feature here is that you may specify another option -k 30s (before the 1s above) in order to kill it with a SIGKILL after another 30 seconds, should it not respond to the original SIGTERM.

    A very useful tool. Now scroll down and up-vote @wRAR’s answer.


    For posterity, this was my original – inferior – suggestion, it might still be if some use for someone.

    A simple bash-script should be able to do that for you

    ./myProgram &
    sleep 1
    kill $! 2>/dev/null && echo "myProgram didn't finish"
    

    That ought to do it.

    $! expands to the last backgrounded process (through the use of &), and kill returns false if it didn’t kill any process, so the echo is only executed if it actually killed something.

    2>/dev/null redirects kill’s stderr, otherwise it would print something telling you it was unable to kill the process.

    You might want to add a -KILL or whichever signal you want to use to get rid of your process too.

    EDIT
    As ephemient pointed out, there’s a race here if your program finishes and the some other process snatches the pid, it’ll get killed instead. To reduce the probability of it happening, you could react to the SIGCHLD and not try to kill it if that happens. There’s still chance to kill the wrong process, but it’s very remote.

    trapped=""
    trap 'trapped=yes' SIGCHLD
    ./myProgram &
    sleep 1
    [ -z "$trapped" ] && kill $! 2>/dev/null && echo '...'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The previous answer is incorrect, if you want to see… May 15, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer While the AsyncTask is working I want to show a… May 15, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer If you're attached to having the codes be sequential, you'll… May 15, 2026 at 6:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.