You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Imran Raza Khan <im...@gmail.com> on 2019/11/13 19:53:52 UTC

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

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

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

Posted by Zoran Regvart <zo...@regvart.com>.
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 <im...@gmail.com> 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

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

Posted by Claus Ibsen <cl...@gmail.com>.
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 <im...@gmail.com> 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