LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-05-2007, 06:33 AM   #1
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Rep: Reputation: 0
cron jobs


Hi,

i have a procedure which i execute daily. this procedure fills up the database with data from another application containing unicode characters. so far, i have been executing this procedure manually. i thought i should use crontabs to do this job for me.

when the cron started, it did all the required jobs BUT all the unicode characters in the database converted into ugly boxes and the whole database was messed up.

what should i do to avoid such behaviour of the crontabs?

kind regards,
Bilal
 
Old 04-05-2007, 07:10 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
cron simply runs scripts that are stored in cron.daily, cron.hourly, etc. If you have a script that is misbehaving, that is not likely a cron issue.

Are you saying that script will run correctly if called manually, but not if called by cron?
 
Old 04-05-2007, 07:40 AM   #3
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
yes, thats right. when i execute the same .sh file manually, it works fine. but thru cron, it messes up the whole database with boxes in place of unicode characters.


Quote:
Originally Posted by pixellany
cron simply runs scripts that are stored in cron.daily, cron.hourly, etc. If you have a script that is misbehaving, that is not likely a cron issue.

Are you saying that script will run correctly if called manually, but not if called by cron?
 
Old 04-05-2007, 12:55 PM   #4
SteveT
Member
 
Registered: Oct 2003
Location: South East UK
Distribution: Fedora Core 16
Posts: 66

Rep: Reputation: 15
Environment

Sounds like you have some form of environment variable that is set in your standard log in that is not being set in the cron environment (in the crontab). Do you have any specific 'language' environment set when you log in?
 
Old 04-06-2007, 05:40 AM   #5
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
yes, a special user was created for this job and environment language was set to utf-8. the crontab executes under a different user but cron user has been added to the same group as the application user.

Quote:
Originally Posted by SteveT
Sounds like you have some form of environment variable that is set in your standard log in that is not being set in the cron environment (in the crontab). Do you have any specific 'language' environment set when you log in?
 
Old 04-06-2007, 08:34 AM   #6
SteveT
Member
 
Registered: Oct 2003
Location: South East UK
Distribution: Fedora Core 16
Posts: 66

Rep: Reputation: 15
Get an env

Ok,
can you then get and 'env' from your environment and the cron environment and compare them?
IE from your shell prompt do env |sort > /tmp/myenv
and then from the cron job thats being run also do env |sort > /tmp/cronenv
then diff the two env's and see what you get.
 
Old 04-06-2007, 09:30 AM   #7
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
env difference

yes, i think you have got to the root of the problem. when i do env .. using the application user, i can see LANG=en_US.UTF-8 line. whereas, the file produced by crontab does not include any information on language.


Quote:
Originally Posted by SteveT
Ok,
can you then get and 'env' from your environment and the cron environment and compare them?
IE from your shell prompt do env |sort > /tmp/myenv
and then from the cron job thats being run also do env |sort > /tmp/cronenv
then diff the two env's and see what you get.
 
Old 04-06-2007, 09:43 AM   #8
SteveT
Member
 
Registered: Oct 2003
Location: South East UK
Distribution: Fedora Core 16
Posts: 66

Rep: Reputation: 15
Crontab

Ok, then you have two or three choices.
If the LANG variable should be:
1) set for all users and jobs
then look at setting it in /etc/profile
2) set for all the cron jobs
then set the variable in the crontab itself (ie sudo crontab -e and insert a line such as LANG=blah)
3) set for just the specific job then export the LANG variable in the job that cron runs

Bon chance
 
Old 04-25-2007, 12:31 AM   #9
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
Thumbs down cron.daily

Dear Steve,

I know nothing about linux. could you please send me the steps/cron statements which will do the following job.

- i need to execute a file /app/myproc.sh daily at 11:00 AM
- before this file is executed, i need to set LANG variable to UTF-8

i guess i need to write this script in /etc/cron.daily directory.

kind regards,
Bilal

Quote:
Originally Posted by SteveT
Ok, then you have two or three choices.
If the LANG variable should be:
1) set for all users and jobs
then look at setting it in /etc/profile
2) set for all the cron jobs
then set the variable in the crontab itself (ie sudo crontab -e and insert a line such as LANG=blah)
3) set for just the specific job then export the LANG variable in the job that cron runs

Bon chance
 
Old 04-25-2007, 01:44 AM   #10
SteveT
Member
 
Registered: Oct 2003
Location: South East UK
Distribution: Fedora Core 16
Posts: 66

Rep: Reputation: 15
Bilal,
I'm no expert either...!
To run at a specific time, I think you need to use a specific crontab. Then that begs the question if your script needs to run as root or not.
If you need root privilege, then either use sudo (this is a command available under FC/RedHat that allows you to gain su privilege while executing a command), or su to root and then run crontab (crontab allows you to set up a 'list of jobs' that are to be run on specific days at specific times).
Then edit the crontab and put in the 'pattern' that says when you want to run your job. If the script you quote is sometimes run without the UTF setting, then you'll either need to add the entry into the crontab, or create a wrapper script for your script that sets and exports UTF then run your script.

So, putting that in stages:
1) If you script needs root privilege to run do one of:
a) sudo crontab -e
b) su
b2) crontab -e

That starts you editting the root crontab.

2) If your script does not need root privilege, then you can edit
your own crontab by simply doing:
a) crontab -e

So 1 above edits the root crontab, and 2 edits your crontab (the crontab file will be named based on your log in id). In either case the crontab file will exist in the 'crontabs' directory (under FC4 that is /var/spool/cron). Its a bit confusing but crontab is the command that is used to edit a crontab file. A crontab file is named after the users login (or su account) and is located in a specific directory (look at the man pages for crontab/cron - or look at this link http://www.unixgeeks.org/security/ne...ix/cron-1.html

A point to note here is that crontab -e kicks of vi as the editor, so you have to save the edit as you would in vi (I tend to use ZZ). You will get a message then saying that the crontab has been updated. You can also abort the edit (:q!) without then affecting the crontab file.

OK, so now you can edit a crontab - now what do you want to put in it?
If you can add the export of the LANG setting to your myproc.sh script (ie your script always runs with LANG set to UTF-8) then simply edit that script and add in the LANG setting. Then add a line to the crontab (via crontab -e) along the lines of:

00 11 * * * /app/myproc.sh

That will run /app/myproc.sh at 11am every day (see the cron/crontab man page for the various time settings). If however, you only want the UTF setting for the 11AM run, then create another script that does:

#! /bin/sh
LANG=UTF-8
export LANG
/app/myproc.sh

Save that as say, /app/myprocUTF.sh, than put that in the crontab via crontab -e, so you have:


00 11 * * * /app/myprocUTF.sh

Finally, if all the jobs that you want to run via cron (crontab) need UTF set then you can edit the respective crontab file (crontab -e) and add the following:

# -- Make sure the lang is set for all crontab jobs .............
LANG=UTF-8

00 11 * * * /app/myproc.sh


That then automatically sets LANG before running any of the jobs.

HIH
 
Old 04-25-2007, 07:49 AM   #11
bilal_linux
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
Hi Steve,

Thanks for all your help. i was doing exactly what you have mentioned here but still it wasnt working. finally, i added the following lines to the .sh file and everything started working:

instead of using /app/myproc.sh, i used two statements cd app and then ./myproc.sh

and it worked

Thanks again
Kind regards,
Bilal


Quote:
Originally Posted by SteveT
Bilal,
I'm no expert either...!
To run at a specific time, I think you need to use a specific crontab. Then that begs the question if your script needs to run as root or not.
If you need root privilege, then either use sudo (this is a command available under FC/RedHat that allows you to gain su privilege while executing a command), or su to root and then run crontab (crontab allows you to set up a 'list of jobs' that are to be run on specific days at specific times).
Then edit the crontab and put in the 'pattern' that says when you want to run your job. If the script you quote is sometimes run without the UTF setting, then you'll either need to add the entry into the crontab, or create a wrapper script for your script that sets and exports UTF then run your script.

So, putting that in stages:
1) If you script needs root privilege to run do one of:
a) sudo crontab -e
b) su
b2) crontab -e

That starts you editting the root crontab.

2) If your script does not need root privilege, then you can edit
your own crontab by simply doing:
a) crontab -e

So 1 above edits the root crontab, and 2 edits your crontab (the crontab file will be named based on your log in id). In either case the crontab file will exist in the 'crontabs' directory (under FC4 that is /var/spool/cron). Its a bit confusing but crontab is the command that is used to edit a crontab file. A crontab file is named after the users login (or su account) and is located in a specific directory (look at the man pages for crontab/cron - or look at this link http://www.unixgeeks.org/security/ne...ix/cron-1.html

A point to note here is that crontab -e kicks of vi as the editor, so you have to save the edit as you would in vi (I tend to use ZZ). You will get a message then saying that the crontab has been updated. You can also abort the edit (:q!) without then affecting the crontab file.

OK, so now you can edit a crontab - now what do you want to put in it?
If you can add the export of the LANG setting to your myproc.sh script (ie your script always runs with LANG set to UTF-8) then simply edit that script and add in the LANG setting. Then add a line to the crontab (via crontab -e) along the lines of:

00 11 * * * /app/myproc.sh

That will run /app/myproc.sh at 11am every day (see the cron/crontab man page for the various time settings). If however, you only want the UTF setting for the 11AM run, then create another script that does:

#! /bin/sh
LANG=UTF-8
export LANG
/app/myproc.sh

Save that as say, /app/myprocUTF.sh, than put that in the crontab via crontab -e, so you have:


00 11 * * * /app/myprocUTF.sh

Finally, if all the jobs that you want to run via cron (crontab) need UTF set then you can edit the respective crontab file (crontab -e) and add the following:

# -- Make sure the lang is set for all crontab jobs .............
LANG=UTF-8

00 11 * * * /app/myproc.sh


That then automatically sets LANG before running any of the jobs.

HIH
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Jobs christo512 Linux - Software 1 02-09-2006 09:19 AM
Cron Jobs XaViaR Linux - General 1 06-23-2005 06:57 PM
cron jobs working2hard Linux - Software 0 07-28-2004 05:31 PM
CRON Jobs scottpioso Linux - Software 63 11-28-2003 06:45 PM
cron jobs Zyanid Linux - Software 0 08-27-2002 05:10 AM


All times are GMT -5. The time now is 12:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration