sfnix/ upstart
Symfony包,用于轻松配置Upstart。它有助于使任何脚本在Linux上后台永久运行。
README
#UpstartBundle 为Symfony 2和3。##关于 这是一个用于轻松配置Symfony的Upstart的bundle。它有助于使任何symfony命令(或任何其他脚本)在后台永久运行并在失败时重启。最常见的此类脚本示例是队列 消费者,另一个示例是WebSocket服务器。##安装 使用composer安装bundle及其依赖项
$ composer require sfnix/upstart
注册bundle
// app/AppKernel.php public function registerBundles() { //... $bundles = [ //... new SfNix\UpstartBundle\UpstartBundle(), ]; //... }
##使用 将upstart
部分添加到您的配置文件中
# app/config/config.yml imports: # ... - { resource: upstart.yml }
# app/config/upstart.yml upstart: project: imagin default: verbose: 1 native: respawn: true setuid: www-data chdir: %kernel.root_dir%/.. job: websocket: tag: [server] script: php bin/chat-server.php imageResizer: tag: [image] quantity: 10 command: rabbitmq:consumer imageResizer -w faceRecognizer: tag: [image] quantity: 5 native: exec: python faceRecognizer.py killSignal: SIGKILL test: command: upstart:test --error 10 verbose: 3
生成并安装从您的配置生成的upstart文件。它还将尝试为该bundle的其他命令启用bash自动完成。
# ./app/console upstart:install
列出作业及其状态。可以使用--watch
(-w
)选项连续显示状态。默认刷新间隔为1秒,请参阅--interval
(-i
)选项。
# ./app/console upstart:list
启动作业。
# ./app/console upstart:start
停止作业。
# ./app/console upstart:stop
停止并启动作业。
# ./app/console upstart:restart
显示作业日志的尾部。可以使用--watch
(-w
)选项连续显示日志。如果您以任何方式重定向脚本输出,则需要使用log
,logDir
作业配置选项。
# ./app/console upstart:log
删除由该bundle生成的upstart文件和bash自动完成脚本。
# ./app/console upstart:delete
您可以使用多个作业名称或标记作为任何命令的参数,以筛选此命令将操作的作业。
# ./app/console upstart:stop websocket image
您可以在作业配置中使用测试命令作为脚本,用于学习目的。默认情况下,它将永久运行,但请查看--exit
和--error
选项。
# ./app/console upstart:test --error 10 -vvv
##具有bash自动完成支持的独立控制台应用程序。此bundle包括独立的控制台应用程序./bin/upstart
(Symfony 2)或./vendor/bin/upstart
(Symfony 3)。此bundle的每个命令都可通过此应用程序使用,无需“upstart:”命名空间。upstart
还支持命令的选项、作业名称和标记
的bash自动完成
。要开始使用bash自动完成,您必须运行upstart install
,然后注销并重新登录。使用方法
# ./bin/upstart install # exit # ./bin/upstart <Tab> # ./bin/upstart start <Tab>
##完整配置参考 阅读有关所有原生段落的详细描述的Upstart Cookbook。
upstart: # Project name will be used as a directory name for upstart config files, and as a prefix for event names. project: ~ # Required, Example: sfnix # Root directory of upstart config files. configDir: /etc/init # Defaults for all jobs. default: # Symfony command debug option (debug: 0 -> --no-debug). It will be ignored if job has no "command". debug: ~ # Symfony command verbose level option (verbose: 0 -> no options, verbose: 3 -> -vvv). It will be ignored if job has no "command". verbose: ~ # Symfony command env option (env: prod -> --env prod) It will be ignored if job has no "command". env: dev # If you use any output redirection for the script, # this option can help you tell the bundle where the log directory is. logDir: /var/log/upstart # Native upstart stanzas. They always will overwrite any stanzas generated by this bundle. native: # This stanza will tell Upstart to ignore the start on / stop on stanzas. manual: ~ # Example: true #This stanza defines the set of Events that will cause the Job to be automatically started. #Syntax: EVENT [[KEY=]VALUE]... [and|or...] startOn: ~ # Example: event1 and runlevel [2345] and (local-filesystems and net-device-up IFACE!=lo) #This stanza defines the set of Events that will cause the Job to be automatically stopped if it is already running. #Syntax: EVENT [[KEY=]VALUE]... [and|or...] stopOn: ~ # Example: runlevel [016] or event2 # Allows an environment variable to be set which is accessible in all script sections. env: # Examples: myVar1: Hello world! myVar2: Goodby world! # Export variables previously set with env to all events that result from this job. export: # Examples: - myVar1 - myVar2 #Used to change Upstart's idea of what a "normal" exit status is. #Conventionally, processes exit with status 0 (zero) to denote success and non-zero to denote failure. normalExit: ~ #Example: [0, 13, SIGUSR1, SIGWINCH] #With this stanza, whenever the main script/exec exits, #without the goal of the job having been changed to stop, #the job will be started again. #This includes running pre-start, post-start and post-stop. #Note that pre-stop will not be run. respawn: ~ # Example: true #Syntax: [int $COUNT, int $INTERVAL] | ["unlimited"] #Respawning is subject to a limit. #If the job is respawned more than COUNT times in INTERVAL seconds, #it will be considered to be having deeper problems and will be stopped. respawnLimit: ~ #In concept, a task is just a short lived job. #In practice, this is accomplished by changing how the transition from a goal of "stop" to "start" is handled. # #Without the 'task' keyword, the events that cause the job to start will be unblocked as soon as the job is started. #This means the job has emitted a starting(7) event, run its pre-start, begun its script/exec, and post-start, #and emitted its started(7) event. # #With task, the events that lead to this job starting will be blocked until the job has completely transitioned back to stopped. #This means that the job has run up to the previously mentioned started(7) event, and has also completed its post-stop, #and emitted its stopped(7) event. task: ~ # Example: true # Load specified AppArmor Mandatory Access Control system profile into the kernel prior to starting the job. # The main job process (as specified by exec or script) will be confined to this profile. # Notes: # - <profile-path> must be an absolute path. # - The job will fail if the profile doesn't exist, or the profile fails to load. apparmorLoad: ~ # Run main job process with already-loaded AppArmor Mandatory Access Control system profile. # Notes: # - The job will fail if the profile named does not exist, or is not already loaded. apparmorSwitch: ~ # Upstart 1.13 supports cgroups with the aid of cgmanager (see cgmanager(8)). # A new "cgroup" stanza is introduced that allows job processes to be run within the specified cgroup. # http://upstart.ubuntu.com/cookbook/#cgroup cgroup: # Examples: - [cgroup, cpu] - [memory, $UPSTART_CGROUP, limit_in_bytes, 52428800] console: ~ # One of "logged"; "output"; "owner"; "none" # Runs the job's processes with a working directory in the specified directory instead of the root of the filesystem. chdir: ~ # Runs the job's processes in a chroot(8) environment underneath the specified directory. chroot: ~ # Provides the ability to specify resource limits for a job. # 0: limit # One of "core"; "cpu"; "data"; "fsize"; "memlock"; "msgqueue"; "nice"; "nofile"; "nproc"; "rss"; "rtprio"; "sigpending"; "stack" # 1: soft limit # integer or "unlimited" # 2: hard limit # integer or "unlimited" limit: # Examples: - [cpu, unlimited, unlimited] # Change the jobs scheduling priority from the default. See nice(1). nice: ~ # Linux has an "Out of Memory" killer facility. # Normally the OOM killer regards all processes equally, this stanza advises the kernel to treat this job differently. # The "adjustment" value provided to this stanza may be an integer value from -999 (very unlikely to be killed by the OOM killer) # up to 1000 (very likely to be killed by the OOM killer). # It may also be the special value never to have the job ignored by the OOM killer entirely # (potentially dangerous unless you really trust the application in all possible system scenarios). oomScore: ~ # Changes to the group before running the job's process. setgid: ~ # Changes to the user before running the job's process. setuid: ~ # Set the file mode creation mask for the process. Value should be an octal value for the mask. See umask(2) for more details. umask: ~ # fork - Upstart will expect the process executed to call fork(2) exactly once. # daemon - Upstart will expect the process executed to call fork(2) exactly twice. # stop - Specifies that the job's main process will raise the SIGSTOP signal to indicate that it is ready. # init(8) will wait for this signal and then: # - Immediately send the process SIGCONT to allow it to continue. # - Run the job's post-start script (if any). # Only then will Upstart consider the job to be running. expect: ~ # One of "fork"; "deamon"; "stop" # Specifies the stopping signal, SIGTERM by default, a job's main process will receive when stopping the running job. killSignal: ~ # One of "SIGHUP"; "SIGINT"; "SIGQUIT"; "SIGILL"; "SIGTRAP"; "SIGIOT"; "SIGBUS"; "SIGFPE"; "SIGKILL"; "SIGUSR1"; "SIGSEGV"; "SIGUSR2"; "SIGPIPE"; "SIGALRM"; "SIGTERM"; "SIGSTKFLT"; "SIGCHLD"; "SIGCONT"; "SIGSTOP"; "SIGTSTP"; "SIGTTIN"; "SIGTTOU"; "SIGURG"; "SIGXCPU"; "SIGXFSZ"; "SIGVTALRM"; "SIGPROF"; "SIGWINCH"; "SIGIO"; "SIGPWR" # The number of seconds Upstart will wait before killing a process. The default is 5 seconds. killTimeout: ~ # Specifies the signal that Upstart will send to the jobs main process when the job needs to be reloaded (the default is SIGHUP). reloadSignal: ~ # One of "SIGHUP"; "SIGINT"; "SIGQUIT"; "SIGILL"; "SIGTRAP"; "SIGIOT"; "SIGBUS"; "SIGFPE"; "SIGKILL"; "SIGUSR1"; "SIGSEGV"; "SIGUSR2"; "SIGPIPE"; "SIGALRM"; "SIGTERM"; "SIGSTKFLT"; "SIGCHLD"; "SIGCONT"; "SIGSTOP"; "SIGTSTP"; "SIGTTIN"; "SIGTTOU"; "SIGURG"; "SIGXCPU"; "SIGXFSZ"; "SIGVTALRM"; "SIGPROF"; "SIGWINCH"; "SIGIO"; "SIGPWR" # Quoted name (and maybe contact details) of author of this Job Configuration File. author: ~ # List of jobs. job: - # Name of a job. Will be used as upstart config file name and log file name. name: ~ # Tags. tag: [] # Number of job instances to run. quantity: 1 # Symfony command. command: ~ # Example: rabbitmq:consumer imageResizer -w # Symfony command debug option (debug: 0 -> --no-debug) debug: ~ # Symfony command verbose level option (verbose: 0 -> no options, verbose: 3 -> -vvv) verbose: ~ # Symfony command env option (env: prod -> --env prod) env: ~ # Run some shell script, not a symfony command. # This is a shortcut for native:{exec:"..."}, or native:{script:"..."}. script: ~ # Example: php bin/websocket-server.php # If you use any output redirection for the script, # this option can help you tell the bundle where the log directory is. logDir: ~ # If you use any output redirection for the script, # this option can help you tell the bundle what is log file base name. log: ~ # Native upstart stanzas. They always will overwrite any stanzas generated by this bundle. native: # This stanza will tell Upstart to ignore the start on / stop on stanzas. manual: ~ # Example: true #This stanza defines the set of Events that will cause the Job to be automatically started. #Syntax: EVENT [[KEY=]VALUE]... [and|or...] startOn: ~ # Example: event1 and runlevel [2345] and (local-filesystems and net-device-up IFACE!=lo) #This stanza defines the set of Events that will cause the Job to be automatically stopped if it is already running. #Syntax: EVENT [[KEY=]VALUE]... [and|or...] stopOn: ~ # Example: runlevel [016] or event2 # Allows an environment variable to be set which is accessible in all script sections. env: # Examples: myVar1: Hello world! myVar2: Goodby world! # Export variables previously set with env to all events that result from this job. export: # Examples: - myVar1 - myVar2 # Stanza that allows the specification of a single-line command to run. exec: ~ # Example: /usr/bin/my-daemon --option foo -v # Use this stanza to prepare the environment for the job. preStart: ~ # Example: [ -d "/var/cache/squid" ] || squid -k # Script or process to run after the main process has been spawned, but before the started(7) event has been emitted. postStart: ~ # Example: while ! mysqladmin ping localhost ; do sleep 1 ; done # The pre-stop stanza will be executed before the job's stopping(7) event is emitted and before the main process is killed. preStop: ~ # Example: /some/directory/script #There are times where the cleanup done in pre-start is not enough. #Ultimately, the cleanup should be done both pre-start and post-stop, #to ensure the service starts with a consistent environment, #and does not leave behind anything that it shouldn. postStop: ~ # Example: /some/directory/script # Allows the specification of a multi-line block of shell code to be executed. Block is terminated by end script. script: ~ # Example: /some/directory/script >> /var/log/some-log.log #Used to change Upstart's idea of what a "normal" exit status is. #Conventionally, processes exit with status 0 (zero) to denote success and non-zero to denote failure. normalExit: ~ #Example: [0, 13, SIGUSR1, SIGWINCH] #With this stanza, whenever the main script/exec exits, #without the goal of the job having been changed to stop, #the job will be started again. #This includes running pre-start, post-start and post-stop. #Note that pre-stop will not be run. respawn: ~ # Example: true #Syntax: [int $COUNT, int $INTERVAL] | ["unlimited"] #Respawning is subject to a limit. #If the job is respawned more than COUNT times in INTERVAL seconds, #it will be considered to be having deeper problems and will be stopped. respawnLimit: ~ #In concept, a task is just a short lived job. #In practice, this is accomplished by changing how the transition from a goal of "stop" to "start" is handled. # #Without the 'task' keyword, the events that cause the job to start will be unblocked as soon as the job is started. #This means the job has emitted a starting(7) event, run its pre-start, begun its script/exec, and post-start, #and emitted its started(7) event. # #With task, the events that lead to this job starting will be blocked until the job has completely transitioned back to stopped. #This means that the job has run up to the previously mentioned started(7) event, and has also completed its post-stop, #and emitted its stopped(7) event. task: ~ # Example: true #Sometimes you want to run the same job, but with different arguments. #The variable that defines the unique instance of this job is defined with instance. instance: ~ # Load specified AppArmor Mandatory Access Control system profile into the kernel prior to starting the job. # The main job process (as specified by exec or script) will be confined to this profile. # Notes: # - <profile-path> must be an absolute path. # - The job will fail if the profile doesn't exist, or the profile fails to load. apparmorLoad: ~ # Run main job process with already-loaded AppArmor Mandatory Access Control system profile. # Notes: # - The job will fail if the profile named does not exist, or is not already loaded. apparmorSwitch: ~ # Upstart 1.13 supports cgroups with the aid of cgmanager (see cgmanager(8)). # A new "cgroup" stanza is introduced that allows job processes to be run within the specified cgroup. # http://upstart.ubuntu.com/cookbook/#cgroup cgroup: # Examples: - [cgroup, cpu] - [memory, $UPSTART_CGROUP, limit_in_bytes, 52428800] console: ~ # One of "logged"; "output"; "owner"; "none" # Runs the job's processes with a working directory in the specified directory instead of the root of the filesystem. chdir: ~ # Runs the job's processes in a chroot(8) environment underneath the specified directory. chroot: ~ # Provides the ability to specify resource limits for a job. limit: # Examples: - [cpu, unlimited, unlimited] # Change the jobs scheduling priority from the default. See nice(1). nice: ~ # Linux has an "Out of Memory" killer facility. # Normally the OOM killer regards all processes equally, this stanza advises the kernel to treat this job differently. # The "adjustment" value provided to this stanza may be an integer value from -999 (very unlikely to be killed by the OOM killer) # up to 1000 (very likely to be killed by the OOM killer). # It may also be the special value never to have the job ignored by the OOM killer entirely # (potentially dangerous unless you really trust the application in all possible system scenarios). oomScore: ~ # Changes to the group before running the job's process. setgid: ~ # Changes to the user before running the job's process. setuid: ~ # Set the file mode creation mask for the process. Value should be an octal value for the mask. See umask(2) for more details. umask: ~ # fork - Upstart will expect the process executed to call fork(2) exactly once. # daemon - Upstart will expect the process executed to call fork(2) exactly twice. # stop - Specifies that the job's main process will raise the SIGSTOP signal to indicate that it is ready. # init(8) will wait for this signal and then: # - Immediately send the process SIGCONT to allow it to continue. # - Run the job's post-start script (if any). # Only then will Upstart consider the job to be running. expect: ~ # One of "fork"; "deamon"; "stop" # Specifies the stopping signal, SIGTERM by default, a job's main process will receive when stopping the running job. killSignal: ~ # One of "SIGHUP"; "SIGINT"; "SIGQUIT"; "SIGILL"; "SIGTRAP"; "SIGIOT"; "SIGBUS"; "SIGFPE"; "SIGKILL"; "SIGUSR1"; "SIGSEGV"; "SIGUSR2"; "SIGPIPE"; "SIGALRM"; "SIGTERM"; "SIGSTKFLT"; "SIGCHLD"; "SIGCONT"; "SIGSTOP"; "SIGTSTP"; "SIGTTIN"; "SIGTTOU"; "SIGURG"; "SIGXCPU"; "SIGXFSZ"; "SIGVTALRM"; "SIGPROF"; "SIGWINCH"; "SIGIO"; "SIGPWR" # The number of seconds Upstart will wait before killing a process. The default is 5 seconds. killTimeout: ~ # Specifies the signal that Upstart will send to the jobs main process when the job needs to be reloaded (the default is SIGHUP). reloadSignal: ~ # One of "SIGHUP"; "SIGINT"; "SIGQUIT"; "SIGILL"; "SIGTRAP"; "SIGIOT"; "SIGBUS"; "SIGFPE"; "SIGKILL"; "SIGUSR1"; "SIGSEGV"; "SIGUSR2"; "SIGPIPE"; "SIGALRM"; "SIGTERM"; "SIGSTKFLT"; "SIGCHLD"; "SIGCONT"; "SIGSTOP"; "SIGTSTP"; "SIGTTIN"; "SIGTTOU"; "SIGURG"; "SIGXCPU"; "SIGXFSZ"; "SIGVTALRM"; "SIGPROF"; "SIGWINCH"; "SIGIO"; "SIGPWR" # Quoted name (and maybe contact details) of author of this Job Configuration File. author: ~ # One line quoted description of Job Configuration File. description: ~ # Specifies the events the job configuration file generates (directly or indirectly via a child process). # This stanza can be specified multiple times for each event emitted. # This stanza can also use the following shell wildcard meta-characters to simplify the specification: # - asterisk ("*") # - question mark ("?") # - square brackets ("[" and "]") emits: ~ # Example: [*-device-*, foo-event, bar-event] # This stanza may contain version information about the job, such as revision control or package version number. # It is not used or interpreted by init(8) in any way. version: ~ # Brief message explaining how to start the job in question. # Most useful for instance jobs which require environment variable parameters to be specified before they can be started. usage: ~