{"id":320,"date":"2025-08-10T10:56:02","date_gmt":"2025-08-10T10:56:02","guid":{"rendered":"https:\/\/blog.chaiyakul.com\/?p=320"},"modified":"2025-08-10T11:09:02","modified_gmt":"2025-08-10T11:09:02","slug":"restart-service-with-php-centos-9","status":"publish","type":"post","link":"https:\/\/blog.chaiyakul.com\/index.php\/2025\/08\/10\/restart-service-with-php-centos-9\/","title":{"rendered":"\u0e43\u0e0a\u0e49 PHP \u0e40\u0e1e\u0e37\u0e48\u0e2d \u0e2b\u0e22\u0e38\u0e14\u0e41\u0e25\u0e30\u0e40\u0e23\u0e34\u0e48\u0e21 service \u0e43\u0e2b\u0e21\u0e48\u0e43\u0e19 CentOS 9"},"content":{"rendered":"\n<p>\u0e04\u0e38\u0e13\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49 PHP \u0e40\u0e1e\u0e37\u0e48\u0e2d \u0e2b\u0e22\u0e38\u0e14\u0e41\u0e25\u0e30\u0e40\u0e23\u0e34\u0e48\u0e21 service \u0e43\u0e2b\u0e21\u0e48\u0e43\u0e19 CentOS 9 \u0e44\u0e14\u0e49 \u0e42\u0e14\u0e22\u0e43\u0e0a\u0e49 shell_exec() \u0e2b\u0e23\u0e37\u0e2d exec() \u0e0b\u0e36\u0e48\u0e07\u0e08\u0e30\u0e23\u0e31\u0e19\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e02\u0e2d\u0e07\u0e23\u0e30\u0e1a\u0e1a Linux \u0e1c\u0e48\u0e32\u0e19 PHP<\/p>\n\n\n\n<p>\u2705 \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e42\u0e04\u0e49\u0e14 PHP: \u0e2b\u0e22\u0e38\u0e14\u0e41\u0e25\u0e30\u0e40\u0e23\u0e34\u0e48\u0e21 service \u0e43\u0e2b\u0e21\u0e48<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n$services = &#x5B;\n    &#039;home411cam01.service&#039;,\n    &#039;home411cam02.service&#039;,\n    &#039;home411cam03.service&#039;,\n    &#039;home411cam05.service&#039;,\n    &#039;home411cam06.service&#039;,\n    &#039;home411cam10.service&#039;\n];\n\nforeach ($services as $service) {\n    \/\/ \u0e2b\u0e22\u0e38\u0e14 service\n    shell_exec(&quot;sudo systemctl stop $service&quot;);\n\n    \/\/ \u0e40\u0e23\u0e34\u0e48\u0e21 service \u0e43\u0e2b\u0e21\u0e48\n    shell_exec(&quot;sudo systemctl start $service&quot;);\n\n    \/\/ \u0e2b\u0e23\u0e37\u0e2d\u0e08\u0e30\u0e43\u0e0a\u0e49 restart \u0e41\u0e17\u0e19\u0e01\u0e47\u0e44\u0e14\u0e49\n    \/\/ shell_exec(&quot;sudo systemctl restart $service&quot;);\n    \n    echo &quot;Restarted $service\\n&quot;;\n}\n?&gt;\n<\/pre><\/div>\n\n\n<h6 class=\"wp-block-heading\">\u2705 \u0e2a\u0e34\u0e48\u0e07\u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e17\u0e33\u0e40\u0e1e\u0e34\u0e48\u0e21:<\/h6>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u0e2a\u0e34\u0e17\u0e18\u0e34\u0e4c sudo<\/strong>: \u0e40\u0e27\u0e47\u0e1a\u0e40\u0e0b\u0e34\u0e23\u0e4c\u0e1f\u0e40\u0e27\u0e2d\u0e23\u0e4c (Apache\/Nginx) \u0e17\u0e35\u0e48\u0e23\u0e31\u0e19 PHP \u0e15\u0e49\u0e2d\u0e07\u0e21\u0e35\u0e2a\u0e34\u0e17\u0e18\u0e34\u0e4c <code>sudo<\/code> \u0e42\u0e14\u0e22\u0e44\u0e21\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e16\u0e32\u0e21\u0e23\u0e2b\u0e31\u0e2a\u0e1c\u0e48\u0e32\u0e19<\/li>\n\n\n\n<li><strong>\u0e41\u0e01\u0e49\u0e44\u0e02 sudoers<\/strong>:<br>\u0e40\u0e1b\u0e34\u0e14\u0e44\u0e1f\u0e25\u0e4c <code>\/etc\/sudoers<\/code> \u0e14\u0e49\u0e27\u0e22\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo visudo\n<\/pre><\/div>\n\n\n<p>\u0e41\u0e25\u0e49\u0e27\u0e40\u0e1e\u0e34\u0e48\u0e21\u0e1a\u0e23\u0e23\u0e17\u0e31\u0e14 (\u0e41\u0e17\u0e19 <code>apache<\/code> \u0e14\u0e49\u0e27\u0e22 user \u0e17\u0e35\u0e48\u0e23\u0e31\u0e19 PHP \u0e40\u0e0a\u0e48\u0e19 <code>www-data<\/code>, <code>nginx<\/code>, \u0e2b\u0e23\u0e37\u0e2d <code>apache<\/code>)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napache ALL=(ALL) NOPASSWD: \/bin\/systemctl\n<\/pre><\/div>\n\n\n<p>     3. \u0e04\u0e27\u0e32\u0e21\u0e1b\u0e25\u0e2d\u0e14\u0e20\u0e31\u0e22: \u0e2b\u0e25\u0e35\u0e01\u0e40\u0e25\u0e35\u0e48\u0e22\u0e07\u0e01\u0e32\u0e23\u0e40\u0e1b\u0e34\u0e14\u0e43\u0e2b\u0e49\u0e1c\u0e39\u0e49\u0e43\u0e0a\u0e49\u0e08\u0e32\u0e01\u0e40\u0e27\u0e47\u0e1a\u0e40\u0e23\u0e35\u0e22\u0e01\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e19\u0e35\u0e49\u0e15\u0e23\u0e07 \u0e46 \u0e40\u0e1e\u0e23\u0e32\u0e30\u0e2d\u0e32\u0e08\u0e16\u0e39\u0e01\u0e42\u0e08\u0e21\u0e15\u0e35\u0e44\u0e14\u0e49<\/p>\n\n\n\n<p>\u2705 \u0e17\u0e32\u0e07\u0e40\u0e25\u0e37\u0e2d\u0e01: \u0e43\u0e0a\u0e49 <code>restart<\/code> \u0e41\u0e17\u0e19<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nshell_exec(&quot;sudo systemctl restart $service&quot;);\n<\/pre><\/div>\n\n\n<p>\u0e08\u0e30\u0e2a\u0e30\u0e14\u0e27\u0e01\u0e01\u0e27\u0e48\u0e32\u0e40\u0e1e\u0e23\u0e32\u0e30\u0e23\u0e27\u0e21 <code>stop<\/code> + <code>start<\/code> \u0e40\u0e02\u0e49\u0e32\u0e44\u0e27\u0e49\u0e14\u0e49\u0e27\u0e22\u0e01\u0e31\u0e19<\/p>\n\n\n\n<p>\u0e44\u0e14\u0e49\u0e40\u0e25\u0e22\u0e04\u0e23\u0e31\u0e1a! \u0e14\u0e49\u0e32\u0e19\u0e25\u0e48\u0e32\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07 <strong>HTML + PHP<\/strong> \u0e41\u0e1a\u0e1a\u0e07\u0e48\u0e32\u0e22\u0e17\u0e35\u0e48\u0e21\u0e35\u0e1b\u0e38\u0e48\u0e21\u0e43\u0e2b\u0e49\u0e01\u0e14\u0e40\u0e1e\u0e37\u0e48\u0e2d <code>restart<\/code> service \u0e17\u0e35\u0e25\u0e30\u0e15\u0e31\u0e27 \u0e2b\u0e23\u0e37\u0e2d\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14<\/p>\n\n\n\n<p>\ud83d\udd27 <code>index.html<\/code> \u2013 \u0e2b\u0e19\u0e49\u0e32\u0e40\u0e27\u0e47\u0e1a HTML \u0e1b\u0e38\u0e48\u0e21<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;meta charset=&quot;UTF-8&quot;&gt;\n  &lt;title&gt;Restart Services&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h2&gt;Restart Camera Services&lt;\/h2&gt;\n  &lt;form method=&quot;post&quot; action=&quot;restart.php&quot;&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam01.service&quot;&gt;Restart cam01&lt;\/button&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam02.service&quot;&gt;Restart cam02&lt;\/button&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam03.service&quot;&gt;Restart cam03&lt;\/button&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam05.service&quot;&gt;Restart cam05&lt;\/button&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam06.service&quot;&gt;Restart cam06&lt;\/button&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;home411cam10.service&quot;&gt;Restart cam10&lt;\/button&gt;\n    &lt;br&gt;&lt;br&gt;\n    &lt;button type=&quot;submit&quot; name=&quot;service&quot; value=&quot;all&quot;&gt;Restart All Services&lt;\/button&gt;\n  &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>\ud83e\udde0 <code>restart.php<\/code> \u2013 \u0e23\u0e31\u0e19\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 PHP \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e23\u0e35\u0e2a\u0e15\u0e32\u0e23\u0e4c\u0e17 service<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n$all_services = &#x5B;\n    &#039;home411cam01.service&#039;,\n    &#039;home411cam02.service&#039;,\n    &#039;home411cam03.service&#039;,\n    &#039;home411cam05.service&#039;,\n    &#039;home411cam06.service&#039;,\n    &#039;home411cam10.service&#039;\n];\n\n$selected = $_POST&#x5B;&#039;service&#039;] ?? &#039;&#039;;\n\nif ($selected === &#039;all&#039;) {\n    foreach ($all_services as $service) {\n        shell_exec(&quot;sudo systemctl restart $service&quot;);\n        echo &quot;Restarted $service&lt;br&gt;&quot;;\n    }\n} elseif (in_array($selected, $all_services)) {\n    shell_exec(&quot;sudo systemctl restart $selected&quot;);\n    echo &quot;Restarted $selected&lt;br&gt;&quot;;\n} else {\n    echo &quot;Invalid service name!&quot;;\n}\n?&gt;\n<\/pre><\/div>\n\n\n<h6 class=\"wp-block-heading\">\ud83d\udd10 \u0e2d\u0e22\u0e48\u0e32\u0e25\u0e37\u0e21:<\/h6>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0e15\u0e31\u0e49\u0e07\u0e2a\u0e34\u0e17\u0e18\u0e34\u0e4c\u0e43\u0e2b\u0e49 web server (\u0e40\u0e0a\u0e48\u0e19 apache, nginx) \u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49 <code>sudo systemctl<\/code> \u0e44\u0e14\u0e49\u0e42\u0e14\u0e22\u0e44\u0e21\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e43\u0e0a\u0e49\u0e23\u0e2b\u0e31\u0e2a\u0e1c\u0e48\u0e32\u0e19<\/li>\n\n\n\n<li>\u0e40\u0e1e\u0e34\u0e48\u0e21\u0e43\u0e19 <code>sudoers<\/code>:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo visudo\n<\/pre><\/div>\n\n\n<p>\u0e41\u0e25\u0e49\u0e27\u0e43\u0e2a\u0e48:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napache ALL=(ALL) NOPASSWD: \/bin\/systemctl\n<\/pre><\/div>\n\n\n<p>\u0e40\u0e1b\u0e25\u0e35\u0e48\u0e22\u0e19 <code>apache<\/code> \u0e15\u0e32\u0e21\u0e0a\u0e37\u0e48\u0e2d\u0e1c\u0e39\u0e49\u0e43\u0e0a\u0e49\u0e17\u0e35\u0e48\u0e40\u0e27\u0e47\u0e1a\u0e04\u0e38\u0e13\u0e23\u0e31\u0e19\u0e2d\u0e22\u0e39\u0e48 (\u0e40\u0e0a\u0e48\u0e19 <code>www-data<\/code>, <code>nginx<\/code>)<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0e04\u0e38\u0e13\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49 PHP \u0e40\u0e1e\u0e37\u0e48\u0e2d \u0e2b\u0e22\u0e38\u0e14\u0e41\u0e25\u0e30\u0e40\u0e23\u0e34\u0e48\u0e21 service \u0e43\u0e2b\u0e21\u0e48\u0e43\u0e19 Cent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","footnotes":""},"categories":[105,162,9,161,160],"tags":[],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-centos","category-html","category-linux","category-php","category-service"],"_links":{"self":[{"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":1,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":321,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions\/321"}],"wp:attachment":[{"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.chaiyakul.com\/index.php\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}