{"id":12858,"date":"2023-09-27T23:33:21","date_gmt":"2023-09-28T06:33:21","guid":{"rendered":"https:\/\/www.apolonio.com\/blog\/?p=12858"},"modified":"2023-09-27T23:33:21","modified_gmt":"2023-09-28T06:33:21","slug":"python-as-a-service","status":"publish","type":"post","link":"https:\/\/www.apolonio.com\/blog\/?p=12858","title":{"rendered":"Python As A Service"},"content":{"rendered":"\n<p>In Centos 7 I have been running screen on reboot (via cron) to start a script like this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@reboot screen -d -m -S pir \/opt\/bin\/myscustom.py<\/pre>\n\n\n\n<p>But wanted to be more modern and use systemd unit files and as of Rocky 8 (Recall I am moving things to Rocky 9) I can run a python script as a systemd service and output the contents to a file for tracking.<\/p>\n\n\n\n<p>So I went off and created a system service (unit) file <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vi \/etc\/systemd\/system\/mycustom.service<\/pre>\n\n\n\n<p>In it I placed <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\r\nDescription=Home Node PIR MQTT\r\nAfter=network.target\r\n\r\n[Service]\r\nUser=larry\r\nGroup=larry\r\nType=simple\r\nStandardError=journal\r\nStandardOutput=journal\r\nStandardInput=null\r\nStandardOutput=file:\/var\/log\/myscustom\/stdout.log\r\nStandardError=file:\/var\/log\/myscustom\/stderr.log\r\nExecStart=\/opt\/bin\/myscustom.py\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n\n\n\n<p>Note instead of file I could have used append so the file just grows instead of getting replaced everytime things are restarted.<\/p>\n\n\n\n<p>I did create the folder and chown it with larry:larry but it created the files on restart with owner root so I chowned the files themselves afterwards.<\/p>\n\n\n\n<p>Another change is the python script itself<\/p>\n\n\n\n<p>I need to add a -u to the first line like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!\/usr\/bin\/python -u<\/pre>\n\n\n\n<p>That tells python to not buffer standard out and send it immediately, that way systemd can see it.<\/p>\n\n\n\n<p>On the personal front, son had his Senior day for cross country, where they celebrate the seniors.  He did OK today, last year rocking something under 21 would be awesome but I guess I expect more.<\/p>\n\n\n\n<p>Not getting my steps in, been super tired.  I need to sleep earlier.<\/p>\n\n\n\n<p>Weight: 344.2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Centos 7 I have been running screen on reboot (via cron) to start a script like this. @reboot screen -d -m -S pir \/opt\/bin\/myscustom.py But wanted to be more modern and use systemd unit files and as of Rocky &hellip; <a href=\"https:\/\/www.apolonio.com\/blog\/?p=12858\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,14,11,3,12,9],"tags":[],"class_list":["post-12858","post","type-post","status-publish","format-standard","hentry","category-fitbit","category-new-toys","category-technical","category-training","category-virtualization","category-weighin"],"_links":{"self":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12858"}],"version-history":[{"count":1,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12858\/revisions"}],"predecessor-version":[{"id":12859,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12858\/revisions\/12859"}],"wp:attachment":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}