Wednesday, September 9, 2015

bash - Run bash_profile in cron job



I had a problem that my cron script wasnt loading the environment variables so I added the following line:




#!/bin/sh
bash /root/.bash_profile


This seems to have no effect and the path variables are not being set correctly.
How do I run the bash_profile script


Answer



bash /root/.bash_profile will create a new bash process, run that file (and I assume set your env variables while doing so), and then exit, taking those newly set env variables with it.




Try . /root/.bash_profile. Or source /root/.bash_profile. That will execute the file in the current bash process, so your env variables will be available to the current process.



You may also want to use #!/bin/bash instead of #!/bin/sh if you're going to do bash-style invocations.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...