{"id":77,"date":"2024-02-10T19:22:26","date_gmt":"2024-02-10T19:22:26","guid":{"rendered":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=77"},"modified":"2024-02-10T19:22:26","modified_gmt":"2024-02-10T19:22:26","slug":"making-daemons-with-python","status":"publish","type":"post","link":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=77","title":{"rendered":"Making Daemons with Python"},"content":{"rendered":"\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">This short project of writing and managing a daemon will provide many benefits for me going forward in any setting. Although this particular daemon will showcase the ability of a program that runs automatically in the background, I will use it in a future project developing a dynamic firewall using Suricata.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">So let&#8217;s look at an example daemon I made named <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">python_daemon1.py<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"587\" height=\"411\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/python_daemon1.py-.png\" alt=\"\" class=\"wp-image-79\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/python_daemon1.py-.png 587w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/python_daemon1.py--300x210.png 300w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">As you can see, I started with importing modules necessary for proper times and dates when logging. <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">Debug<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> is a variable that we have declared if we want to come back for debugging. I put a <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">count<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> variable outside of the while loop equal to 1. Inside the while loop which runs forever, I use a try\/except block. So it will <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">&#8216;try&#8217;<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> the code underneath the block and if it works with no errors, it will skip the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">except<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> block. If it fails, it will immediately move to the first line of the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">except<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> block.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The first line is a variable called <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">the_time<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> which gives us the standard utc time. The next string variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">log_entry<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, will enter the time and count from the variables on the end of the string. Next, we make a new variable called &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">f<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216; that opens my <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">python_daemon1.log<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> file in the \/var\/log directory. I use the &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">a<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216; function to open it and add to it. Next is <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">f.write<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> to write the log entry string into it. It will then close that file. The following is the debug which if set to true, will print out my different stuff along the way. I next increment my <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">count<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> by one and end it with a <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">time.sleep<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> argument of 5 which means it takes 5 seconds to do nothing before it cycles over it again.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Inside the &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">except<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216; block, it is very similar to the &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">try<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216; block but it will write an error to the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">python_daemon1_error.log<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> in a new variable called &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">g<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216;. In that error log will be the time of error and new line breaks before the traceback comment. The most important in the &#8216;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">except<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8216; block is the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">traceback.format_exc<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> which can pull the full traceback error for understanding. This helps with very long scripts and gives real-time comments. It then closes and is followed by the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">time.sleep<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> argument which sits for 60 seconds before trying again.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The rest of this post is how to manage and configure a daemon manager so I don&#8217;t have to manually do it every time I get onto my virtual environment. With that said, I started by installing supervisor which is a daemon manager that does it all.<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"775\" height=\"161\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/install-supervisor.png\" alt=\"\" class=\"wp-image-80\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/install-supervisor.png 775w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/install-supervisor-300x62.png 300w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/install-supervisor-768x160.png 768w\" sizes=\"auto, (max-width: 775px) 100vw, 775px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Next, I needed to tell supervisor to run my program so I headed to the \/etc\/supervisor directory to find all configuration files.  I then created a daemon for the conf.d directory which is pictured below.  The program name will be daemon as you see.  <\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"263\" height=\"163\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/python_daemon.conf-script.png\" alt=\"\" class=\"wp-image-81\"\/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">As you see by the command, I needed to move my <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">python_daemon1.py<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> to the \/opt directory which is a good place for add-on software application packages.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">From here, the next steps include restarting and rereading the supervisor service to put my daemon in there. As you see below, it is up and running!<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"565\" height=\"71\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/sudo-supervisorctl.png\" alt=\"\" class=\"wp-image-82\" style=\"width:840px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/sudo-supervisorctl.png 565w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/sudo-supervisorctl-300x38.png 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Lastly, after some research on best practices, it was advised to restart them daily. With that said, I used another daemon manager called Cron which will run commands in intervals for you. So I ran a cron command and opened it using option 2 so I could add a line using VIM.<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"457\" height=\"178\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/crontab.png\" alt=\"\" class=\"wp-image-84\" style=\"width:693px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/crontab.png 457w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/crontab-300x117.png 300w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"444\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-85\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/image.png 654w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/image-300x204.png 300w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">I added the single line on the bottom that will restart all my daemons once a day to ensure everything is working properly.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The final piece is now checking if my daemon is working. Let&#8217;s check out the log file!<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"252\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/daemon-confirmation.png\" alt=\"\" class=\"wp-image-86\" style=\"width:507px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/daemon-confirmation.png 363w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/02\/daemon-confirmation-300x208.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">As you can see, all the information we asked for in our original script is outputted with the date, time, and five second intervals before going through it again. Some takeaways from this project included skills in:<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Python Daemon Creation<\/mark><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Cron<\/mark><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Supervisor<\/mark><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Error Handling<\/mark><\/li>\n<\/ul>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Thanks for following along.<\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This short project of writing and managing a daemon will provide many benefits for me going forward in any setting. Although this particular daemon will showcase the ability of a program that runs automatically in the background, I will use it in a future project developing a dynamic firewall using Suricata. So let&#8217;s look at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"[]"},"categories":[1],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":1,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}