LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2013, 08:01 AM   #1
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Rep: Reputation: 26
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.

Last edited by gacanepa; 12-17-2013 at 08:02 AM.
 
Old 12-17-2013, 08:08 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,492

Rep: Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429
you can do something like this:
env > env.sh
edit env.sh if you want to do so (drop unnecessary lines...)
edit /home/me/run_lines.sh, insert the line:
source /full/path/to/env.sh
at the beginning of that script.
now your script will be able to use that environment.
also insert set -xv at the beginning to see what's happening

Last edited by pan64; 12-17-2013 at 08:09 AM. Reason: typo
 
1 members found this post helpful.
Old 12-17-2013, 08:16 AM   #3
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Original Poster
Rep: Reputation: 26
Hey pan64,
It worked like a charm.
Thank you so very much for taking the time to help (I already marked your post as helpful by clicking on Yes ).
Have a nice day!
 
Old 12-17-2013, 08:17 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,492

Rep: Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429
you are welcome
 
  


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 job is not recognizing environment variables Jon Blanchard Linux - Newbie 6 02-17-2012 10:50 PM
run cron script only for logged in user r_jr Linux - General 1 11-23-2011 05:09 PM
can run script from my user but not with cron beeblequix Linux - Enterprise 2 04-13-2007 11:22 AM
cron jobs environment variables Alfar Linux - General 4 12-15-2006 02:47 PM
Cron Losing Environment Variables? SteveT Fedora 1 12-24-2005 09:37 AM


All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration