bash - How can I set the terminal encoding when running a crontab job?

07
2014-07
  • Konstantin

    I have a crontab job which executes a Ruby script on Arch Linux. It works basically, but when I call "googlecl" to post to my blog, it always shows a "safeDecodeError" exception (eg.: googlecl.SafeDecodeError: ascii could not decode 'Vas\xc3\xa1rnap'), because the command line I use contains national hungarian characters (éáüö, etc). My locale is set hu_HU.UTF-8 by command:

    sudo localectl set-locale LANG="hu_HU.UTF-8"
    

    My crontab line looks like:

    05 15 * * * export LANG="hu_HU.UTF-8"; export LC_CTYPE="hu_HU.UTF-8"; /home/walaki/dl/musor-blogger
    

    My Ruby script looks like:

    #!/usr/bin/ruby
    # encoding: utf-8
    
    require 'watir-webdriver'
    require 'pry'
    
    reklam=["http://keresztenytars.net","http://premium-leechers.blogspot.hu"]
    
    fname="/home/walaki/util/radiomusor-blogger.txt"
    b = Watir::Browser.new :phantomjs
    if ARGV.length==0 then 
     date_today=Time.now.to_s.split(" ")[0]
    else
     date_today=ARGV[0]
    end 
    
    url="http://hangtar.radio.hu/kossuth#!#"+date_today
    datepart=date_today.gsub("-","")
    dow=Date.parse(date_today).strftime("%A")
     case dow
         when /monday/i
          nap="Hétfő"
         when /tuesday/i
          nap="Kedd"
         when /wednesday/i
          nap="Szerda"
         when /thursday/i
          nap="Csütörtök"
         when /friday/i
          nap="Péntek"
         when /saturday/i
          nap="Szombat"
         when /sunday/i
          nap="Vasárnap"
     end
    
    puts url
    puts nap
    puts datepart
    b.goto url
    b.links(:text=>"Részletes műsor")[0].click
    sleep 6
    #html="";b.divs(:class=>"musorelem sclick").each{|d| d.click rescue nil;html+=b.html}
    html=b.html
    tc=html.scan(/class\=\"me_idopont.*?\<span\>(.*?)\<\/span\>.*?\<div.*?class\=\"me_cim.*?\<span\>(.*?)\<\/span\>.*?\<div.*?class\=\"me_leiras.*?\>(.*?)\<\/div\>/m).flatten
    h={};for i in 0..(tc.length-1)/3; h.merge!(tc[i*3]=>[tc[i*3+1],tc[i*3+2]]);end
    
       open(fname, 'w') { |f|
              f.puts "<div class='post-body entry-content' itemprop='articleBody'>"
              f.puts "<b>"+nap+"</b><br>"
              h.each do |k,v|
                #line="<a href=http://stream001.radio.hu:443/stream/"+datepart+"_"+k.gsub(":","")+"00_1.mp3>"+k+" "+v[0]+"</a><br><p style='margin: 0px 0px 0px 15px; text-align:justify; line-height:10px'><small>"+v[1].gsub("<br>","\n")+"</small></p>"
                line="<a href=http://stream001.radio.hu:443/stream/#{datepart}_#{k.gsub(":","")}00_1.mp3>#{k}</a> <a href='#{reklam.sample(1).first}' target=_blank>#{v[0]}</a><br><p style='margin: 0px 0px 0px 15px; text-align:justify; line-height:10px'><small>#{v[1].gsub("<br>","\n")}</small></p>"
                #binding.pry
                #puts line
                f.puts line
              end
             f.puts "</div>"
            }
    
    
    var='google blogger post --blog="My Blog" --tags="'+nap+'" --title="'+date_today+', '+nap+'" --src=/home/walaki/util/radiomusor-blogger.txt'
    
    system var
    

    When I call the googlecl command with Ruby's "system" statement, the command line (var) contains UTF-8 characters (áéüö, etc) and googlecl throw an exception.

    When I call my Ruby script from the usual terminal window manually, it works perfectly. But when I use crontab to start it daily, it always fails due to this terminal character encoding issue.

    My question is how can I set the terminal character encoding when running the cron job?

  • Answers
  • sburlappp

    Two approaches to try:

    • Make sure you re-export LANG and other environment variables for your subshell to make sure the "google" command sees them too.

    http://stackoverflow.com/questions/8301294/shell-out-from-ruby-while-setting-an-environment-variable

    • Encode the day-of-week strings ("nap") with HTML codes (&#n;) since they're going to the web anyway:

    http://en.wikipedia.org/wiki/Unicode_and_HTML


  • Related Question

    osx - Cron job on Mac not running during sleep
  • Full Decent

    I have a cron job that will mail a list of all Google Tasks to my email address daily at 7am. (http://privacylog.blogspot.com/2010/07/api-for-google-tasks.html)

    I am running this on my laptop which, like me, is in sleep mode at 7am.

    Can I fix this to run after I wake the machine from sleep each day?


  • Related Answers
  • mipadi

    If you use launchd instead of cron, tasks that were supposed to have ran while your Mac was sleeping will run when it wakes up. There's some more info (including usage) available here.

    (For what it's worth, Apple recommends using launchd instead of cron, and may even phase out cron at some point in the future.)