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

The Archive Base Latest Questions

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

I’d like a daemonizer that can turn an arbitrary, generic script or command into

  • 0

I’d like a daemonizer that can turn an arbitrary, generic script or command into a daemon.

There are two common cases I’d like to deal with:

  1. I have a script that should run forever. If it ever dies (or on reboot), restart it. Don’t let there ever be two copies running at once (detect if a copy is already running and don’t launch it in that case).

  2. I have a simple script or command line command that I’d like to keep executing repeatedly forever (with a short pause between runs). Again, don’t allow two copies of the script to ever be running at once.

Of course it’s trivial to write a “while(true)” loop around the script in case 2 and then apply a solution for case 1, but a more general solution will just solve case 2 directly since that applies to the script in case 1 as well (you may just want a shorter or no pause if the script is not intended to ever die (of course if the script really does never die then the pause doesn’t actually matter)).

Note that the solution should not involve, say, adding file-locking code or PID recording to the existing scripts.

More specifically, I’d like a program “daemonize” that I can run like

% daemonize myscript arg1 arg2

or, for example,

% daemonize 'echo `date` >> /tmp/times.txt'

which would keep a growing list of dates appended to times.txt. (Note that if the argument(s) to daemonize is a script that runs forever as in case 1 above, then daemonize will still do the right thing, restarting it when necessary.) I could then put a command like above in my .login and/or cron it hourly or minutely (depending on how worried I was about it dying unexpectedly).

NB: The daemonize script will need to remember the command string it is daemonizing so that if the same command string is daemonized again it does not launch a second copy.

Also, the solution should ideally work on both OS X and linux but solutions for one or the other are welcome.

EDIT: It’s fine if you have to invoke it with sudo daemonize myscript myargs.

(If I’m thinking of this all wrong or there are quick-and-dirty partial solutions, I’d love to hear that too.)


PS: In case it’s useful, here’s a similar question specific to python.

And this answer to a similar question has what appears to be a useful idiom for a quick-and-dirty demonizing of an arbitrary script:

  • 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:59:08+00:00Added an answer on May 13, 2026 at 10:59 pm

    You can daemonize any executable in Unix by using nohup and the & operator:

    nohup yourScript.sh script args&
    

    The nohup command allows you to shut down your shell session without it killing your script, while the & places your script in the background so you get a shell prompt to continue your session. The only minor problem with this is standard out and standard error both get sent to ./nohup.out, so if you start several scripts in this manor their output will be intertwined. A better command would be:

    nohup yourScript.sh script args >script.out 2>script.error&
    

    This will send standard out to the file of your choice and standard error to a different file of your choice. If you want to use just one file for both standard out and standard error you can us this:

    nohup yourScript.sh script args >script.out 2>&1 &
    

    The 2>&1 tells the shell to redirect standard error (file descriptor 2) to the same file as standard out (file descriptor 1).

    To run a command only once and restart it if it dies you can use this script:

    #!/bin/bash
    
    if [[ $# < 1 ]]; then
        echo "Name of pid file not given."
        exit
    fi
    
    # Get the pid file's name.
    PIDFILE=$1
    shift
    
    if [[ $# < 1 ]]; then
        echo "No command given."
        exit
    fi
    
    echo "Checking pid in file $PIDFILE."
    
    #Check to see if process running.
    PID=$(cat $PIDFILE 2>/dev/null)
    if [[ $? = 0 ]]; then
        ps -p $PID >/dev/null 2>&1
        if [[ $? = 0 ]]; then
            echo "Command $1 already running."
            exit
        fi
    fi
    
    # Write our pid to file.
    echo $$ >$PIDFILE
    
    # Get command.
    COMMAND=$1
    shift
    
    # Run command until we're killed.
    while true; do
        $COMMAND "$@"
        sleep 10 # if command dies immediately, don't go into un-ctrl-c-able loop
    done
    

    The first argument is the name of the pid file to use. The second argument is the command. And all other arguments are the command’s arguments.

    If you name this script restart.sh this is how you would call it:

    nohup restart.sh pidFileName yourScript.sh script args >script.out 2>&1 &
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've solved the problem by making a .cpp file with… May 14, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer The way I see it is you have 2 options… May 14, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer This is a decent tutorial for creating a Settings Bundle… May 14, 2026 at 7:38 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.