Environment variable not accessible if camel run in Cron Job of CentOS

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Environment variable not accessible if camel run in Cron Job of CentOS

imranrazakhan
I have one jar file which contain camel route to run it from cron job i
created shell script

public void process(Exchange exchange) throws Exception
    {
        String host =
exchange.getContext().resolvePropertyPlaceholders("{{env:HOSTNAME}}");
}

test.sh
#!/bin/sh
java -jar my.jar

Now when i run

sh test.sh it is able to access environment variable, but when i run from
cron job it throw null pointer exception for  {{env:HOSTNAME}}

Following is my cron

cat > /etc/cron.d/reboot << EOF
* * * * * root /usr/bin/flock -w 1 -E 0 /tmp -c /test.sh 2>&1 |
/usr/bin/tee /dev/tty1
EOF
Reply | Threaded
Open this post in threaded view
|

Re: Environment variable not accessible if camel run in Cron Job of CentOS

Claus Ibsen-2
Hi

Hmm I wonder if its something with permission issue, eg run cron vs normal.

Camel uses the JDK api to get env variables from System.getenv, so you
can try some java code that does that and see if you can get those
from the cron job or not.

On Wed, Nov 13, 2019 at 8:54 PM Imran Raza Khan <[hidden email]> wrote:

>
> I have one jar file which contain camel route to run it from cron job i
> created shell script
>
> public void process(Exchange exchange) throws Exception
>     {
>         String host =
> exchange.getContext().resolvePropertyPlaceholders("{{env:HOSTNAME}}");
> }
>
> test.sh
> #!/bin/sh
> java -jar my.jar
>
> Now when i run
>
> sh test.sh it is able to access environment variable, but when i run from
> cron job it throw null pointer exception for  {{env:HOSTNAME}}
>
> Following is my cron
>
> cat > /etc/cron.d/reboot << EOF
> * * * * * root /usr/bin/flock -w 1 -E 0 /tmp -c /test.sh 2>&1 |
> /usr/bin/tee /dev/tty1
> EOF



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Reply | Threaded
Open this post in threaded view
|

Re: Environment variable not accessible if camel run in Cron Job of CentOS

Zoran Regvart-2
In reply to this post by imranrazakhan
Hi Imran,
$HOSTNAME is not POSIX[1], so depending on the shell being used it is
present in the environment or not. bash[2] defines it other shells
might not. If you're running the cron process under another user their
shell might be used.

Perhaps it would be easier to try
`InetAddress.getLocalHost().getHostName()` or have a look at some of
the solutions offered on stackoverflow[3].

zoran

[1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
[2] https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Variables
[3] https://stackoverflow.com/questions/7348711/recommended-way-to-get-hostname-in-java

On Wed, Nov 13, 2019 at 8:54 PM Imran Raza Khan <[hidden email]> wrote:

>
> I have one jar file which contain camel route to run it from cron job i
> created shell script
>
> public void process(Exchange exchange) throws Exception
>     {
>         String host =
> exchange.getContext().resolvePropertyPlaceholders("{{env:HOSTNAME}}");
> }
>
> test.sh
> #!/bin/sh
> java -jar my.jar
>
> Now when i run
>
> sh test.sh it is able to access environment variable, but when i run from
> cron job it throw null pointer exception for  {{env:HOSTNAME}}
>
> Following is my cron
>
> cat > /etc/cron.d/reboot << EOF
> * * * * * root /usr/bin/flock -w 1 -E 0 /tmp -c /test.sh 2>&1 |
> /usr/bin/tee /dev/tty1
> EOF



--
Zoran Regvart
Reply | Threaded
Open this post in threaded view
|

Re: Environment variable not accessible if camel run in Cron Job of CentOS

imranrazakhan
In reply to this post by imranrazakhan
Hi Zoran,

Thanks for detailed answer and it was really helpful, For others i tried
following options


   - source $HOME/.bash_profile
   - 0 5 * * * . $HOME/. bash_profile ; /path/to/command/to/run
   - set following in cron

   SHELL=/bin/bash


But nothing works for me in centos, it look like hostname variable is not
available , So then i used

InetAddress.getLocalHost().getHostName()


<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virenfrei.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>