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 8399303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:15:57+00:00 2026-06-09T21:15:57+00:00

I want a way to write a daemon in a shell script, which runs

  • 0

I want a way to write a daemon in a shell script, which runs another application in a loop, restarting it if it dies.

  • When run using ./myscript.sh from an SSH session, it shall launch a new instance of the daemon, except if the daemon is already running.
  • When the SSH session ends, the daemon shall persist.
  • There shall be a parameter (./myscript -stop) that kills any existing daemon.

(Notes on edit – The original question specified that nohup and similar tools may not be used. This artificial requirement was an “XY question”, and the accepted answer in fact uses all the tools the OP claimed were not possible to use.)

  • 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-06-09T21:15:59+00:00Added an answer on June 9, 2026 at 9:15 pm

    Based on clarifications in comments, what you actually want is a daemon process that keeps a child running, relaunching it whenever it exits. You want a way to type “./myscript.sh” in an ssh session and have the daemon started.

    #!/usr/bin/env bash
    PIDFILE=~/.mydaemon.pid
    if [ x"$1" = x-daemon ]; then
      if test -f "$PIDFILE"; then exit; fi
      echo $$ > "$PIDFILE"
      trap "rm '$PIDFILE'" EXIT SIGTERM
      while true; do
        #launch your app here
        /usr/bin/server-or-whatever &
        wait # needed for trap to work
      done
    elif [ x"$1" = x-stop ]; then
      kill `cat "$PIDFILE"`
    else
      nohup "$0" -daemon
    fi
    

    Run the script: it will launch the daemon process for you with nohup. The daemon process is a loop that watches for the child to exit, and relaunches it when it does.

    To control the daemon, there’s a -stop argument the script can take that will kill the daemon. Look at examples in your system’s init scripts for more complete examples with better error checking.

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

Sidebar

Related Questions

I wrote a simple script using python-daemon which prints to sys.stdout : #!/usr/bin/env python
I want to know what is a simplest way to write method which generates
I want to run a Perl script with some while(1) loop in the background
I found a way to write avi from BMP files: http://www.delphi3000.com/articles/article_2770.asp?SK= I want to
Is there any way to write multilanguage documentation using POD? If no, what should
I'm looking to write a PHP script to act as a mini daemon to
I want to write a class which provides basically a list of entries. This
I want to use vim to write a part of my file to another
I want to know what is the best way to write in the URL.py
I want to know the proper way to write a query like this: var

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.