{"id":427,"date":"2025-03-09T20:34:41","date_gmt":"2025-03-09T20:34:41","guid":{"rendered":"https:\/\/www.jibsheet.net\/linux\/?p=427"},"modified":"2025-03-10T01:30:04","modified_gmt":"2025-03-10T01:30:04","slug":"python-subprocess-not-finding-command-in-cron","status":"publish","type":"post","link":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/","title":{"rendered":"python subprocess not finding command in cron."},"content":{"rendered":"<p>I had a script that when I run as my user &#8211; it could find all commands.\u00a0 This was in a Fedora environment.<\/p>\n<p>The solution was to wrap it in shell like this<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[jon2allen@jons-bad-ass-fedora-server-37 python]$ cat run_news.sh\r\n#!\/bin\/bash\r\nsource \/home\/jon2allen\/.bashrc\r\npython3 \/home\/jon2allen\/python\/news1_test.py\r\n<\/pre>\n<p>sourcing the .bashrc<\/p>\n<p>The command has several subprocess shell.\u00a0 before I sourced .bashrc &#8211; yt-dlp could not be found. and fabric. etc.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[jon2allen@jons-bad-ass-fedora-server-37 python]$ cat news1_test.py\r\nimport subprocess\r\nfrom datetime import datetime\r\n\r\nclass CustomDate:\r\n    def __init__(self, date=None):\r\n        if date is None:\r\n            self.date = datetime.now()\r\n        elif isinstance(date, datetime):\r\n            self.date = date\r\n        else:\r\n            raise ValueError(\"Date must be a datetime object or None\")\r\n\r\n    def return_date(self):\r\n        month_abbreviations = {\r\n            1: \"Jan.\",\r\n            2: \"Feb.\",\r\n            3: \"March\",\r\n            4: \"April\",\r\n            5: \"May\",\r\n            6: \"June\",\r\n            7: \"July\",\r\n            8: \"Aug.\",\r\n            9: \"Sept.\",\r\n            10: \"Oct.\",\r\n            11: \"Nov.\",\r\n            12: \"Dec.\"\r\n        }\r\n\r\n        month = month_abbreviations[self.date.month]\r\n        day = self.date.day\r\n        year = self.date.year\r\n\r\n        return f\"{month} {day}, {year}\"\r\n\r\n    def print_date(self):\r\n        print(self.return_date())\r\n\r\n\r\ndef search_youtube(search_query):\r\n\r\n    command_str = f'yt-dlp ytsearch2:\"{search_query}\" --get-id --get-title'\r\n\r\n    try:\r\n        result = subprocess.run(command_str, capture_output=True, shell=True, text=True, check=True)\r\n        lines = result.stdout.strip().split(\"\\n\")\r\n\r\n        if len(lines) != 4:\r\n            raise ValueError(f\"Unexpected output length: expected 4 lines, got {len(lines)}\")\r\n\r\n        # Return the 4 lines as a tuple\r\n        return tuple(lines)\r\n\r\n    except subprocess.CalledProcessError as e:\r\n        raise RuntimeError(f\"yt-dlp command failed with error: {e.stderr}\") from e\r\n    except Exception as e:\r\n        raise RuntimeError(f\"Failed to run yt-dlp command: {e}\") from e\r\n\r\ndef fabric_call(youtubid):\r\n\r\n    command_str = f'yt https:\/\/www.youtube.com\/watch?v={youtubid} | fabric -sp newscast'\r\n\r\n    print (command_str)\r\n\r\n    try:\r\n        result = subprocess.run(command_str, capture_output=True, shell=True, text=True, check=True)\r\n        lines = result.stdout\r\n\r\n\r\n        return (lines)\r\n\r\n    except subprocess.CalledProcessError as e:\r\n        raise RuntimeError(f\"yt\/fabric command failed with error: {e.stderr}\") from e\r\n    except Exception as e:\r\n        raise RuntimeError(f\"Failed to run yt\/fabric command: {e}\") from e\r\n\r\n\r\n\r\n# Example usage\r\nif __name__ == \"__main__\":\r\n    date1 = CustomDate() \r\n    search_str = 'PBS News Hour full episode, ' + date1.return_date()\r\n    print(\"search: \" + search_str) \r\n    result = search_youtube(search_str)\r\n    print(result)\r\n\r\n    if result[0].find('live') &lt; 0 :\r\n         print(\"result0:  \", result[0], result[1])\r\n         if result[0].find(date1.return_date()) &gt; -1:\r\n             print(\"found date\") \r\n             print(fabric_call(result[1]))\r\n    if result[2].find('live') &lt; 0 :\r\n         print(\"result2:  \", result[2], result[3]) \r\n         if result[2].find(date1.return_date()) &gt; -1:\r\n             print(\"found date\" )\r\n             print(fabric_call(result[3]))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This script pulls down the daily PBS News Hour transcript using yt-dlp.\u00a0 and uses fabric to get an ai summary of the newscast.\u00a0 I have to run it round 8pm Eastern.<\/p>\n<p>The fabric prompt:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># IDENTITY and PURPOSE\r\n\r\nYou are an AI assistant tasked with summarizing newscasts. Your role involves meticulously analyzing newscast content to identify major topics and extract key supporting facts.  You will then organize this information into a structured summary, ensuring clarity and accuracy in your representation of the news. You must adhere to specific formatting guidelines to present the summary in a clear and readable manner.  You are responsible for not only understanding the content of the newscast but also interpreting the instructions provided to ensure that your output perfectly matches the requested format and includes the appropriate number of supporting facts for each topic. Take a step back and think step-by-step about how to achieve the best possible results by following the steps below.\r\n\r\n# STEPS\r\n\r\n- Analyze the provided newscast text.\r\n- Identify the major topics discussed.\r\n- Extract 3 to 5 supporting facts for each major topic.\r\n- Organize the summary by topic, listing supporting facts under each.\r\n\r\n\r\n# OUTPUT INSTRUCTIONS\r\n\r\n- Only output Markdown.\r\n- All sections should be Heading level 1.\r\n- Subsections should be one Heading level higher than their parent section.\r\n- All bullets should have their own paragraph.\r\n- Ensure you follow ALL these instructions when creating your output.\r\n\r\n# INPUT\r\n\r\nINPUT:\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a script that when I run as my user &#8211; it could find all commands.\u00a0 This was in a Fedora environment. The solution was to wrap it in shell like this &nbsp; [jon2allen@jons-bad-ass-fedora-server-37 python]$ cat run_news.sh #!\/bin\/bash source \/home\/jon2allen\/.bashrc python3 \/home\/jon2allen\/python\/news1_test.py sourcing the .bashrc The command has several subprocess shell.\u00a0 before I sourced .bashrc &#8211; yt-dlp could not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38,5],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff\" \/>\n<meta property=\"og:description\" content=\"I had a script that when I run as my user &#8211; it could find all commands.\u00a0 This was in a Fedora environment. The solution was to wrap it in shell like this &nbsp; [jon2allen@jons-bad-ass-fedora-server-37 python]$ cat run_news.sh #!\/bin\/bash source \/home\/jon2allen\/.bashrc python3 \/home\/jon2allen\/python\/news1_test.py sourcing the .bashrc The command has several subprocess shell.\u00a0 before I sourced .bashrc &#8211; yt-dlp could not [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\" \/>\n<meta property=\"og:site_name\" content=\"Jon Allen&#039;s Linux Stuff\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-09T20:34:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-10T01:30:04+00:00\" \/>\n<meta name=\"author\" content=\"jonallen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"jonallen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\"},\"author\":{\"name\":\"jonallen\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"headline\":\"python subprocess not finding command in cron.\",\"datePublished\":\"2025-03-09T20:34:41+00:00\",\"dateModified\":\"2025-03-10T01:30:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\"},\"wordCount\":100,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"articleSection\":[\"AI\",\"Linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\",\"url\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\",\"name\":\"python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff\",\"isPartOf\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#website\"},\"datePublished\":\"2025-03-09T20:34:41+00:00\",\"dateModified\":\"2025-03-10T01:30:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.jibsheet.net\/linux\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python subprocess not finding command in cron.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#website\",\"url\":\"https:\/\/www.jibsheet.net\/linux\/\",\"name\":\"Jon Allen's Linux Stuff\",\"description\":\"Howto&#039;s, Observations, and stuff\",\"publisher\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.jibsheet.net\/linux\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\",\"name\":\"jonallen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g\",\"caption\":\"jonallen\"},\"logo\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.jibsheet.net\/linux\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/","og_locale":"en_US","og_type":"article","og_title":"python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff","og_description":"I had a script that when I run as my user &#8211; it could find all commands.\u00a0 This was in a Fedora environment. The solution was to wrap it in shell like this &nbsp; [jon2allen@jons-bad-ass-fedora-server-37 python]$ cat run_news.sh #!\/bin\/bash source \/home\/jon2allen\/.bashrc python3 \/home\/jon2allen\/python\/news1_test.py sourcing the .bashrc The command has several subprocess shell.\u00a0 before I sourced .bashrc &#8211; yt-dlp could not [&hellip;]","og_url":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/","og_site_name":"Jon Allen&#039;s Linux Stuff","article_published_time":"2025-03-09T20:34:41+00:00","article_modified_time":"2025-03-10T01:30:04+00:00","author":"jonallen","twitter_card":"summary_large_image","twitter_misc":{"Written by":"jonallen","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#article","isPartOf":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/"},"author":{"name":"jonallen","@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"headline":"python subprocess not finding command in cron.","datePublished":"2025-03-09T20:34:41+00:00","dateModified":"2025-03-10T01:30:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/"},"wordCount":100,"commentCount":0,"publisher":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"articleSection":["AI","Linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/","url":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/","name":"python subprocess not finding command in cron. - Jon Allen&#039;s Linux Stuff","isPartOf":{"@id":"https:\/\/www.jibsheet.net\/linux\/#website"},"datePublished":"2025-03-09T20:34:41+00:00","dateModified":"2025-03-10T01:30:04+00:00","breadcrumb":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2025\/03\/09\/python-subprocess-not-finding-command-in-cron\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.jibsheet.net\/linux\/"},{"@type":"ListItem","position":2,"name":"python subprocess not finding command in cron."}]},{"@type":"WebSite","@id":"https:\/\/www.jibsheet.net\/linux\/#website","url":"https:\/\/www.jibsheet.net\/linux\/","name":"Jon Allen's Linux Stuff","description":"Howto&#039;s, Observations, and stuff","publisher":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.jibsheet.net\/linux\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993","name":"jonallen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g","caption":"jonallen"},"logo":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.jibsheet.net\/linux"]}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/427"}],"collection":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":4,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/427\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}