Locale settings for your cron job

Do you get special characters problem when executing your bash script from a cron job?
And does the same script work fine when it is directly executed from the command line?
If yes, continue reading this article! 😉

The reason of this characters problem is probably because of your locale settings.
Indeed, If you try to run the command locale from the command line and from a cron job, you may get different results such as:

From the command line From a cron job

As you can see, the cron job is not using UTF-8. That must be the problem! 🙂

So the question now is how to change the locale settings for the cron job?
Some people say that you need to add the following environment variables to the crontab entry:


But this actually didn’t work for me. 🙁

What you can do instead is create (if not already present) the file /etc/environment and add the following line:


The cron process will read this file when it starts, so you need to restart it in order to apply the change:

service cron restart

Hope this will fix your characters problem. 😉

, , , , ,

  1. #1 by weakish on 27 Apr 2010 - 11:50

    /etc/environment is deprecated, but cron somehow need it. 🙁

    Details: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=543895

  2. #2 by jinhao on 07 May 2010 - 19:30

    thx,very useful and clear.

  3. #3 by carlos on 19 Aug 2010 - 15:04

    very clear and useful, since /environment is deprecated, man crontab didnt clear enought about setting a real locale settings

  4. #4 by Tiger on 13 Dec 2011 - 09:54

    Thank you, one shot in google and my scripts works properly 🙂

  5. #5 by Omar Shibli on 05 Jan 2012 - 11:47

    Thanks a lot.
    p.s. the above “service cron restart” command didn’t work for me, so i tried this instead “/etc/rc.d/init.d/crond restart”.

  6. #6 by smoreau on 05 Jan 2012 - 11:57

    Yes, this is perfectly fine to run the command /etc/rc.d/init.d/crond restart instead.
    If you want to use the command service cron restart, you would need to install the service tool using the following command:

    apt-get install sysvconfig
  7. #7 by Goddchen on 08 Feb 2012 - 11:36

    Adding an “export” before the lines in your shell script should do the trick 🙂 At least it worked for me.

  8. #8 by smoreau on 08 Feb 2012 - 11:55

    Thank you for your input Goddchen. 🙂

    However, I don’t think it is the best solution as you would have to add the “export” command to all your shell scripts.
    I prefer to have this problem fixed one for good instead of having to remember to add this command for every script I would create in the future…

  9. #9 by Kaurin on 16 Feb 2012 - 06:00

    Thanks for the article! Helped me out with cleaning up my log file for flexget 🙂

  10. #10 by David on 15 Jan 2013 - 08:51

    Thanks to you, this is very useful.

  11. #11 by ravikant on 29 Jan 2013 - 09:55

    many thanks !! solved the problem straight away

  12. #12 by Murat Çorlu on 25 Feb 2013 - 00:48

    My crontab suggests using /etc/default/locale file instead of deprecated /etc/environment file:

    ○ → /etc/init.d/cron restart
    /etc/environment has been deprecated for locale information; use /etc/default/locale for LANG=tr_TR.UTF-8 instead … (warning).
    /etc/environment has been deprecated for locale information; use /etc/default/locale for LANGUAGE=tr_TR.UTF-8 instead … (warning).
    /etc/environment has been deprecated for locale information; use /etc/default/locale for LC_ALL=tr_TR.UTF-8 instead … (warning).
    Restarting periodic command scheduler: cron.

  13. #13 by Tarek on 18 Apr 2013 - 17:20

    My /etc/default/locale had the correct configuration, but I was still getting this error.

    Creating /etc/environment actually fixed it.

    Quick way to create /etc/environment or /etc/default/locale with your own (as sudo user):

    locale > /etc/environment
    locale > /etc/default/locale

  14. #14 by pgn674 on 22 Aug 2013 - 18:33

    Thank you, this worked on CentOS 5.5. /etc/environment existed, but was empty. The service name on CentOS is crond, by the way.

  15. #15 by Nataraj on 26 Aug 2013 - 09:07

    Thanks to you, this is very useful.

  16. #16 by tagMap on 06 Nov 2013 - 00:25

    I tried to put LANG=en_US.UTF-8 inside the script and that did not work. It worked when I made LANG variable a part of the crontab:

    * * * * * LANG=en_US.UTF-8 yourscript.sh

  17. #17 by Roman on 08 Feb 2016 - 23:50

    Thank you very much. this article helped me a lot 🙂

  18. #18 by Ender on 06 Dec 2016 - 19:10

    Dude, you are awesome, thanks a LOT!

(will not be published)