LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-29-2006, 12:33 PM   #1
laggerific
Member
 
Registered: Jan 2006
Posts: 65

Rep: Reputation: 15
Question loading environmental variables in cron - best practice?


I'm curious what people feel are the most effective ways to load environmental variables for scripts called from cron. I've heard that the scripts themselves should contain them, but potentially limits the ease of their portability, and I've heard that there can be security risks with just loading environmental variables for an entire cron environment.

Any thoughts or suggestions on this would be enlightening and helpful. Thank you.
 
Old 11-29-2006, 01:17 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There's another option, which mitigates some of the maintenance issues of having variables set in all your scripts, yet doesn't require the environment to be altered for all cron jobs. That is to launch your cron jobs via some wrapper script which sets environment variables.

e.g. Make a script /home/me/cronrun containing:
Code:
export PATH="$PATH:/your/cron/path/extras"
export SOMEENV="somevalue you need"
"$@"
Be very careful about sourceing your shell init files (e.g. ~/.bashrc) that you don't end up spawning lots of shells, especially if you have a "tricked out" your shell init files. It'll probably be perfectly OK until some smart-arse makes a change and all your cron jobs stop working, or worse, jam up the system...

Once you've made such a script and chmodeed it to be executable, you can modify your cronjobs like this (addition in blue):
Code:
0 * * * * /home/me/cronrun old_command arg arg

Last edited by matthewg42; 11-29-2006 at 01:18 PM. Reason: added blue for clarity
 
Old 11-30-2006, 10:26 AM   #3
laggerific
Member
 
Registered: Jan 2006
Posts: 65

Original Poster
Rep: Reputation: 15
Smile

Thank you for that excellent suggestion...
 
  


Reply



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
Environmental Variables danglingpointer Slackware 13 01-06-2005 12:36 AM
environmental variables msound Programming 0 08-21-2004 07:06 PM
environmental variables? blackzone Linux - General 1 08-06-2004 03:44 AM
how to set environmental variables chakkerz Linux - Newbie 8 07-01-2004 03:06 AM
environmental variables mbhenry Linux - General 6 09-18-2003 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:22 PM.

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
Open Source Consulting | Domain Registration