How do I run a node.js app as a background service?

前端 未结 26 1093
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:28

Since this post has gotten a lot of attention over the years, I\'ve listed the top solutions per platform at the bottom of this post.


Original post

26条回答
  •  后悔当初
    2020-11-22 06:16

    If you are running OSX, then the easiest way to produce a true system process is to use launchd to launch it.

    Build a plist like this, and put it into the /Library/LaunchDaemons with the name top-level-domain.your-domain.application.plist (you need to be root when placing it):

    
    
    
    
        Label
        top-level-domain.your-domain.application
    
        WorkingDirectory
        /your/preferred/workingdirectory
    
        ProgramArguments
        
            /usr/local/bin/node
            your-script-file
        
    
        RunAtLoad
        
    
        KeepAlive
        
    
    
    
    

    When done, issue this (as root):

    launchctl load /Library/LaunchDaemons/top-level-domain.your-domain.application.plist
    launchctl start top-level-domain.your-domain.application
    

    and you are running.

    And you will still be running after a restart.

    For other options in the plist look at the man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html

提交回复
热议问题