Sybren Sybren - 1 year ago 76
Ruby Question

Encoding issue when setting up CRON as user?

I've set up CRON to run a code that works fine when run directly from the terminal, but fires up errors when run via CRON.

I've appended the cron via

env EDITOR=nano crontab -e

To run a Ruby code as if from the standard terminal user.
The code requires ruby (including 'watir' and 'csv' gems) and chromedriver.

58 10 * * * PATH=$PATH:/usr/local/bin; /Users/sybren/chromedriver; Ruby 'ruby/mycode.rb'

The PATH is there to change (environment) settings to my local user settings.
The '/Users/sybren/chromedriver' is there to load chromedriver as an environment variable. At the end it runs my ruby code ('Ruby 'ruby/mycode.rb'')

It fires up the following error:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1987:in `=~': invalid byte sequence in US-ASCII (ArgumentError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1987:in `init_separators'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1509:in `initialize'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1261:in `new'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1261:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1119:in `foreach'

There is no '~' in my ruby code. And again, the code runs fine when run from terminal. What is going wrong here? I believe it is giving the error when opening a new csv and iterating through it.

Answer Source

In your code where you open and/or read the file content, set the encoding for it explicitly:, encoding: ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download