Introduction
If you want a program or application to run in background, process managers like supervisord, pm2, etc. can get this done for you.
Supervisor
ubuntu 安裝指令
sudo apt-get install supervisor
唯一要注意的是 command=/usr/local/bin/long.sh
command 要寫完整的指令路徑
/etc/supervisor/conf.d/aws-sqs.conf
:
[program:aws-sqs]
command=/home/ubuntu/mygo/bin/aws-sqs
autostart=true
autorestart=true
stderr_logfile=/var/log/golang.err.log
stdout_logfile=/var/log/golang.out.log
stderr_logfile_maxbytes=10MB
user=ubuntu
environment=USER='test',BRAND='Company_name',HOME=/home/my_user
directory=/var/www
將 supervisor 跑起來
sudo service supervisor start
對 program conf 的指令
sudo supervisorctl
> restart worker
> reread
> status
> start resque
> tail resque // error log
- or
sudo supervisorctl reread goworker
- 有時候不管怎麼 restart 及 reread 都沒有用時用 reload
Error CRIT Server 'unix_http_server' running without any HTTP authentication checking
設定 unix_http_server 帳密
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
username = user
password = 123
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
username = user
password = 123
Install
npm install pm2 -g
If you haven’t installed node.js and npm, please take a look
Usage
Monitor a process / Stop / Delete from list
pm2 start test.sh
pm2 stop test.sh
pm2 delete test.sh
List
pm2 list
Pm2 provides some useful modules.
pm2 install <module_name>
- pm2-logrotate : auto rotate logs of PM2 and applications managed
- pm2-webshell : expose a fully capable terminal in browsers
- pm2-server-monit : monitor your server health
Set logrotate
pm2 set pm2-logrotate:max_size 5M The max size limits up to 5MB.
pm2 set pm2-logrotate:retain 10 Preserve 10 log files. (include current one)
pm2 show pm2-logrotate Show the configuration of pm2-logrotate
First a SIGINT a signal is sent to your processes,
signal you can catch to know that your process is going to be stopped.
If your application does not exit by itself before 1.6s customizable
it will receive a SIGKILL signal to force the process exit.
Lenghten timeout of graceful stop
$ pm2 start app.js --kill-timeout 3000 // 3000ms
Test at 2016-12-23: It doesn’t work and I don’t know why. It’s just like that it will wait 3~5 seconds then it force to kill the process.