{"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<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_87_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/guoyunhe.me\/en\/2026\/03\/22\/fix-pm2-killed-by-selinux-systemd\/#The_Problem\" >The Problem<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/guoyunhe.me\/en\/2026\/03\/22\/fix-pm2-killed-by-selinux-systemd\/#The_Cause\" >The Cause<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/guoyunhe.me\/en\/2026\/03\/22\/fix-pm2-killed-by-selinux-systemd\/#The_Solution\" >The Solution<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Problem\"><\/span>The Problem<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\"><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 class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/Unitech\/pm2\/issues\/4813\">https:\/\/github.com\/Unitech\/pm2\/issues\/4813<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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\"><span class=\"ez-toc-section\" id=\"The_Cause\"><\/span>The Cause<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><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\"><span class=\"ez-toc-section\" id=\"The_Solution\"><\/span>The Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\"><\/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}]}}