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?