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

  • SEARCH
  • Home
  • 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 6387387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:08:45+00:00 2026-05-25T03:08:45+00:00

Possible Duplicate: How do I write a bash script to restart a process if

  • 0

Possible Duplicate:
How do I write a bash script to restart a process if it dies?

I’ve made a C program that occasionally crashes and I can’t fix it (Some problem with getaddrinfo which is rather spontaneous seeming). I would like to restart the program upon the crash. I thought this would be easy. I was going to separate the problematic libcurl code with a fork and look up how I can detect a process from closing so it can be forked again. However I went with the “easy” option of trying to restart the entire program and recover data from a file.

I tried this:

#!/bin/sh
while true; do
    cd "~/ProgramDir"
    exec "~/ProgramDir/Program"
done

But when the program exits on a failure it starts outputting the next execution to the terminal input if that makes sense. SO if I pretend my program is just a Hello World program then it would do something like this:

bash-3.2$ start.sh
Hello World!
Hello World!
bus error
bash-3.2$ Hello World!
-bash: Hello: command not found
bash-3.2$ Hello World!
-bash: Hello: command not found

It wont continue the program as before. The terminal thinks the program has exited but then takes the output of the next execution to the terminal input.

What is the proper way to do this?

  • 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-25T03:08:45+00:00Added an answer on May 25, 2026 at 3:08 am
    #!/bin/bash
    
    ~/ProgramDir/Program || exec "$0"
    

    This script would run “~/ProgramDir/Program” and wait for its exit status. If the status is 0 (which means “success” in bash), then the script itself terminates. Otherwise, if the program returns a non-0 value or it terminates unnaturally (e.g. it get killed), the script would launch itself again, like a tail recursion in C.

    Edit: code updated according to R..’s comment

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Image comparison algorithm So basically i need to write a program that
Possible Duplicate: Writing C# Plugin System Is it possible to write a program that
Possible Duplicate: How can I write a Java application that can update itself at
Possible Duplicate: Write a function that returns the longest palindrome in a given string
Possible Duplicate: Can you write object oriented code in C? I'm wondering if it's
Possible Duplicate: Java \ Pattern - how to write a pattern that verifies the
Possible Duplicate: In C++ why can’t I write a for() loop like this: for(
Possible Duplicate: Why split the <script> tag when writing it with document.write()? I have
Possible Duplicate: Why split the <script> tag when writing it with document.write()? I don't
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point

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.