Operating System - HP-UX
1833780 Members
2474 Online
110063 Solutions
New Discussion

Re: pass variable to crontab?

 
Dave Walley
Frequent Advisor

pass variable to crontab?

Hi,
I am trying to pass a variable to a cron job that is currently exported in /etc/profile.
Is there a way to pass the variable transparent to the user (it is a password), apart from running the /etc/profile again within the script?
any theories greatly appreciated
Cheers
Ian
why do i do this to myself
2 REPLIES 2
Thierry Poels_1
Honored Contributor

Re: pass variable to crontab?

hi,

"...apart from running the /etc/profile again ..."
/etc/profile is not sourced automatically for cron jobs. You will have to this explicitely in your cron job.
Placing passwords in files is always a security breach. Personally I think /etc/profile is one of the worst places to put a password in: everyone has access to it.
If you need the password in the cron job you will have the read it from a file somewhere.
regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
James R. Ferguson
Acclaimed Contributor

Re: pass variable to crontab?

Hi Dave:

There are several ways to provide environmental variables needed by a cron task:

In general, environmental variables can be embedded in your script as necessary, or they can be specified and exported or file-sourced in the crontab entry like:

0 1 * * * (V=x;export V;/home/my/script)

0 1 * * * (. /home/my/.env /home/my/script)

0 1 * * * su -c -c /home/my/script

For maintainability, I would centralize environmental variables is one place. For instance, you could maintain them in an "environmenal" file which would be sourced by your profile as well as any script needing them. In this way changes need only be made once.

...JRF...