{"id":295,"date":"2026-03-22T15:19:21","date_gmt":"2026-03-22T07:19:21","guid":{"rendered":"https:\/\/guoyunhe.me\/en\/?p=295"},"modified":"2026-03-22T17:28:58","modified_gmt":"2026-03-22T09:28:58","slug":"fix-pm2-killed-by-selinux-systemd","status":"publish","type":"post","link":"https:\/\/guoyunhe.me\/en\/2026\/03\/22\/fix-pm2-killed-by-selinux-systemd\/","title":{"rendered":"Fix PM2 Killed By SELinux + Systemd"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">The Problem<\/h2>\n\n\n\n<p>When you deploy PM2 managed apps (Node.js, Python, etc.) on Linux system with SELinux + Systemd, you will find the PM2 process got killed every 1~2 minutes. <\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/62814539\/pm2-keeps-getting-killed-every-90-seconds-on-centos-8\">https:\/\/stackoverflow.com\/questions\/62814539\/pm2-keeps-getting-killed-every-90-seconds-on-centos-8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/Unitech\/pm2\/issues\/4813\">https:\/\/github.com\/Unitech\/pm2\/issues\/4813<\/a><\/p>\n\n\n\n<p>Since PM2 itself is killed, all managed Node.js\/Python app instances will be killed, too. PM2 will auto restart after being killed, so this problem is hard to notice. You may see occasionally 502 errors during the restarting time window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Cause<\/h2>\n\n\n\n<p><code>pm2 startup<\/code> will create a systemd system service. This systemd service read pm2 saved data under user's home directory. (Default: \/home\/your_user\/.pm2\/) However, SELinux thinks this is a bad act and deny access, causing system service fail and restart. systemd will send kill signal to PM2, causing PM2 killed and restarted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>The most easy and safe solution is to modify SELinux rules to allow Systemd read <code>~\/.pm2<\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"># 1. Define the rule in the SELinux database\nsudo semanage fcontext -a -t var_run_t \"\/home\/&lt;your_user&gt;\/.pm2(\/.*)?\"\n# 2. Apply the rule to the existing files\nsudo restorecon -Rv \/home\/&lt;your_user&gt;\/.pm2<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem When you deploy PM2 managed apps (Node.js, Python, etc.) on Linux system with SELinux + Systemd, you will find the PM2 process got killed every 1~2 minutes. https:\/\/stackoverflow.com\/questions\/62814539\/pm2-keeps-getting-killed-every-90-seconds-on-centos-8 https:\/\/github.com\/Unitech\/pm2\/issues\/4813 Since PM2 itself is killed, all managed Node.js\/Python app instances will be killed, too. PM2 will auto restart after being killed, so this problem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16],"tags":[33,32,31],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-code","category-linux","tag-nodejs","tag-selinux","tag-systemd"],"_links":{"self":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":10,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/295\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}