bash - "invalid or incomplete multibyte or wide character" when executing script by cron

17,312

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

Are you sure it's LC_TYPE and not LC_CTYPE? The latter should be the correct spelling. What's the other values of the locale variable, POSIX or pl_PL.UTF-8? You can set environment variables directly in the crontab file, just like you do with SHELL etc. Try setting LC_CTYPE and/or LC_ALL and/or LANG to pl_PL.UTF-8. Or perhaps find and remove non-ASCII characters from your script and the data files it uses. – n.m.

You're right, there are LC_CTYPE instead of LC_TYPE. Other variables are POSIX. I've added to script this line: export LC_CTYPE=pl_PL.UTF-8 and it works. Thank you very much for help! – Daniel Gadawski

Share:
17,312
Daniel Gadawski
Author by

Daniel Gadawski

Updated on August 27, 2022

Comments

  • Daniel Gadawski
    Daniel Gadawski over 1 year

    I've got a bash script sending an e-mail using mailx. Mailbox configuration is in .mailrc. I'm running it as a root:

    linux:~ # ./lprojekt.sh
    Resolving host smtp.gmail.com . . . done.
    Connecting to 173.194.70.109 . . . connected.
    220 mx.google.com ESMTP n20sm30980415wiw.5
    >>> EHLO linux.site
    250-mx.google.com at your service, [95.49.133.188]
    250-SIZE 35882577
    250-8BITMIME
    250-STARTTLS
    250 ENHANCEDSTATUSCODES
    >>> STARTTLS
    220 2.0.0 Ready to start TLS
    >>> EHLO linux.site
    250-mx.google.com at your service, [95.49.133.188]
    250-SIZE 35882577
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    >>> AUTH LOGIN
    334 VXNlcm5hbWU6
    >>> bG1mYnlkYjlAZ21haWwuY29t
    334 UGFzc3dvcmQ6
    >>> cHJvamVrdDEyMz==
    235 2.7.0 Accepted
    >>> MAIL FROM: <[email protected]>
    250 2.1.0 OK n20sm30980415wiw.5
    >>> RCPT TO: <[email protected]>
    250 2.1.5 OK n20sm30980415wiw.5
    >>> DATA
    354  Go ahead n20sm30980415wiw.5
    >>> .
    250 2.0.0 OK 1335692819 n20sm30980415wiw.5
    >>> QUIT
    221 2.0.0 closing connection n20sm30980415wiw.5
    

    As you can see, it runs correctly.

    However, there's a problem when I try to run this script using cron.

    Here's my /etc/crontab content:

    SHELL=/bin/bash
    PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
    MAILTO=root
    HOME=/root
    #
    # check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
    #
    -*/15 * * * *   root  test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null
    
    * * * * * root /root/lprojekt.sh >> /root/Desktop/log.txt
    

    Each minute (it's just for debug now), root user get a mail:

    Message 40:
    From [email protected]  Sun Apr 29 13:50:01 2012
    X-Original-To: root
    Delivered-To: [email protected]
    From: [email protected]
    To: [email protected]
    Subject: Cron <root@linux> /root/lprojekt.sh >> /root/Desktop/log.txt
    X-Cron-Env: <SHELL=/bin/bash>
    X-Cron-Env: <PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin>
    X-Cron-Env: <MAILTO=root>
    X-Cron-Env: <HOME=/root>
    X-Cron-Env: <LOGNAME=root>
    X-Cron-Env: <USER=root>
    Date: Sun, 29 Apr 2012 13:50:01 +0200 (CEST)
    
    Invalid or incomplete multibyte or wide character
    . . . message not sent.
    

    What's the reason of this problem? How to resolve it? Thanks in advance for help.

    • n. m.
      n. m. almost 12 years
      Insert an invocation of the locale command to your script, compare the output when run from the terminal and from cron.
    • Daniel Gadawski
      Daniel Gadawski almost 12 years
      One line of the locale output is different: cron: LC_TYPE="POSIX" manual execute: LC_TYPE=pl_PL.UTF-8 How to set LC_TYPE for cron?
    • n. m.
      n. m. almost 12 years
      Are you sure it's LC_TYPE and not LC_CTYPE? The latter should be the correct spelling. What's the other values of the locale variable, POSIX or pl_PL.UTF-8? You can set environment variables directly in the crontab file, just like you do with SHELL etc. Try setting LC_CTYPE and/or LC_ALL and/or LANG to pl_PL.UTF-8. Or perhaps find and remove non-ASCII characters from your script and the data files it uses.
    • Daniel Gadawski
      Daniel Gadawski almost 12 years
      You're right, there are LC_CTYPE instead of LC_TYPE. Other variables are POSIX. I've added to script this line: export LC_CTYPE=pl_PL.UTF-8 and it works. Thank you very much for help!