Run script through cron with user environment variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run script through cron with user environment variables
# 1  
Old 12-17-2013
Run script through cron with user environment variables

Hi everyone,
I wrote a script that is supposed to be run by cron on a daily basis. It works just fine if I run it manually, but due to a lack of environment variables (which are available during my user session but not when cron runs the script) it keeps failing to run successfully.
Here's the complete scenario:
The run_lines.sh script executes the lines.sh script 6 times, each one with different parameters, like this:

run_lines.sh:
Code:
#!/bin/bash
/home/me/lines.sh 04 email1@mydomain.com
/home/me/lines.sh 05 email2@mydomain.com
/home/me/lines.sh 06 email3@mydomain.com

(Only 3 lines are shown above)
And here's the crontab entry:
Code:
15 08 * * * /home/me/run_lines.sh

Now here's what I'm thinking. I created a list of the environment variables that are available to my user session with:
Code:
env > env_user.log

Then I'm thinking about doing this in one of the scripts mentioned above (run_lines.sh or lines.sh):
Code:
while read line; do
export $line
done < env_user.log

That way when the script is run through cron, all the environment variables of my user session will be exported and made available to cron.
I also tried
Code:
15 08 * * * me /home/me/run_lines.sh

but my implementation of cron does not seem to allow that.
I've done it before but I'm not quite sure it's the most efficient way to go.
Any hints or ideas will be more than welcome.
Thanks in advance.
# 2  
Old 12-17-2013
There might be a quoting problem. I, for example, have a variable
Code:
SSH_CLIENT=10.100.xxx.xxx 61150 22

Without quoting you get
Code:
export SSH_CLIENT=10.100.220.201 61150 22
bash: export: `61150': not a valid identifier
bash: export: `22': not a valid identifier

There might be more problems like this.

I think you might be better off identifying the necessary variables and just set them.
# 3  
Old 12-17-2013
[SOLVED] Run script through cron with user environment variables

hergp,
Thanks for your message and for the warning. Yes I noticed the quoting problem but eventually solved it.
I ended up identifying the necessary variables (all related to an Oracle installation) and exported them as you said. Thanks again for taking the time to help. Have a nice day!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX $USER and $LOGNAME environment variables

I have some comments about a previously closed topic whose name is the same as above Omitted from the discussion was the situation with a "sudo command or sudo within a script". There is an inconsistency between systems. On some systems $LOGNAME is invariant, on others, on RedHat sudo... (3 Replies)
Discussion started by: lsatenstein
3 Replies

2. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

3. Solaris

Environment variables and cron

where do you set environment variables for cron jobs? I have a feeling it would ignore ~/.bashrc ? thanks. (2 Replies)
Discussion started by: orange47
2 Replies

4. Shell Programming and Scripting

Setting environment variables in Cron file

Hi, In Cron file i'm using username and password hard-coded and now i wann to use environmental veraiables in cron file. But Could you please guide me how to use these environmental variables in cron file ? Thanks, Shyamu.A (4 Replies)
Discussion started by: shyamu544
4 Replies

5. Shell Programming and Scripting

Unix $USER and $LOGNAME environment variables

Hi, I am wondering what is the difference between the USER and LOGNAME environment variables, and under what situations would they be different? I am using Ubuntu 8.04 32-bit and I do not have 'login' command to test it. (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions: 1. How to write a script in HP-UX 11. 2. How to schedule a script. 3. How to "call" scripts from the original script. 4. How to su to another user from within a script. This is the basics of what the... (15 Replies)
Discussion started by: instant000
15 Replies

7. UNIX for Advanced & Expert Users

FTP run by cron only works if user logged in?

I set up a cron job to FTP to another machine. If I have not logged in before the time the cron is set to run, then the ftp program won't connect. I have run this cron on other boxes (diff networks) and it works fine...it is just this one. If anyone has any suggestions as to what would be... (5 Replies)
Discussion started by: vincaStar
5 Replies

8. UNIX for Dummies Questions & Answers

shell script run by user or cron job ?

My shell script runs fine both as a cron job and when i issue it. However, I wish to differentiate when it runs as a cron-job so the "echo" statements are not issued (they get mailed to me, which i don't want). I tried checking $USER but since the cron was created in my user that does not... (5 Replies)
Discussion started by: sentinel
5 Replies

9. HP-UX

How to use own account env variables to cron user

hi all, i have one account for unix Tru64. i can login this account and i do execute special shell script(login sqlplus, execute another shell, etc... ) on this account on my server. but i can't run this shell from cron tab. i think that become cron user envirnoment variables. (3 Replies)
Discussion started by: Tlg13team
3 Replies

10. Solaris

Setting environment variables within cron jobs!!

Is it possible to use environment variables within cron jobs. I am using a cron job to run a c program at regular intervals. The C program uses a library and i have set the library path in the LD_LIBRARY_PATH environment variable. But when i ran the job i got the error library not found!! Any... (1 Reply)
Discussion started by: atheek
1 Replies
Login or Register to Ask a Question