{"id":17,"date":"2010-09-29T22:23:43","date_gmt":"2010-09-29T22:23:43","guid":{"rendered":"http:\/\/ulrichard.is-a-geek.net\/?p=17"},"modified":"2010-09-29T22:23:43","modified_gmt":"2010-09-29T22:23:43","slug":"missing-environment-variables-when-running-from-cron","status":"publish","type":"post","link":"https:\/\/ulrichard.ch\/blog\/?p=17","title":{"rendered":"missing env variables when running from cron"},"content":{"rendered":"<p>Today I solved another problem that bugged me for a while. Namely, I wanted to run the free flight prediction runs automatically from cron. When I ran it from the commandline or a script, it worked  well, but when I tried to run it directly or through the same script from  cron, it always failed with the following message : &#8220;critical error :  basic_string::_S_construct NULL not valid&#8221;<\/p>\n<p><!--more--><br \/>\nI tried everything that came to my mind and that I found on google, but nothing helped.<\/p>\n<p>Normally, the program would log the progress to stdout. But the redirected log or the email contained nothing than the error message. So, the error must have happened during initialization. Consequently, I looked at the source again, and now I spotted a possible problem.<\/p>\n<p>In the program I used <strong>getenv(&#8220;USER&#8221;)<\/strong> and <strong>getenv(&#8220;HOME&#8221;)<\/strong> which I assumed would always be valid. I run the command as root user in cron. If I type <strong>echo $HOME<\/strong> or <strong>echo $USER<\/strong> at the commandline when logged in as root, it shows the proper values.<br \/>\nBut after adding <strong>HOME=\/root<\/strong> and <strong>USER=root<\/strong> to \/etc\/cron.d\/flightpred it works.<\/p>\n<p>So I added fallbacks to the respective places in the code as well.<br \/>\nI still don&#8217;t understand, why the env vars were not set when running from cron, but I&#8217;m glad it works now.<\/p>\n<p>Btw: You can reach the prediction results at<\/p>\n<p><a title=\"http:\/\/flightpred.homelinux.org\/\" href=\"http:\/\/flightpred.homelinux.org\/\">http:\/\/flightpred.homelinux.org\/<\/a><\/p>\n<p>And today I also added an xml output for use in other applications or apps at :<\/p>\n<p><a title=\"http:\/\/flightpred.homelinux.org\/forecast.php\" href=\"http:\/\/flightpred.homelinux.org\/forecast.php\">http:\/\/flightpred.homelinux.org\/forecast.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I solved another problem that bugged me for a while. Namely, I wanted to run the free flight prediction runs automatically from cron. When I ran it from the commandline or a script, it worked well, but when I tried to run it directly or through the same script from cron, it always failed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,1],"tags":[64,68,85,135,168,230],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-paragliding","category-projects","category-uncategorized","tag-cron","tag-debian","tag-flightpred","tag-linux","tag-paragliding","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ulrichard.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}