乱世@小熊 2009-02-08 06:11 采纳率: 25%
浏览 328
已采纳

如何在 unix 中对任意的脚本进行后台化?

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:

转载于:https://stackoverflow.com/questions/525247/how-do-i-daemonize-an-arbitrary-script-in-unix

  • 写回答

12条回答 默认 最新

  • 10.24 2010-03-11 08:44
    关注

    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 &
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误