I have a ruby script which i want to run as cron job.It runs perfectly fine when i run through command prompt on a linux machine. But when i run that same script using a cron job it fails.

I get following error with string encoding. Not sure why it only complains when i am running as a cron job. I have also set my shell to /bin/bash in crontab, which is same shell as my command prompt.

The error i get is

in `encode': "\xE2" on US-ASCII (Encoding::InvalidByteSequenceError)

the line of code that causes the error is


1 Answer 1


The error message is telling you that you have set, in the crontab environment, US-ASCII as your language, and that in such language the string "\xE2" does not exist. Most likely, this is due to the fact that the string is ASCII-8bit encoded, while you need to convert it to UTF-8.

It is possible that the string was contaminated by some invisible character, as it often happens when you do a cut and paste.

The following, to be applied before manipulating the string, should solve your problem:

  new_str = some_str.encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.