How can I set environment variables for a program executed using `nohup`?

19,822

Three methods:

  • set (and export) the variable before launching mvn

  • set the variable on the nohup launch:

    FORMAVEN=valueForMaven nohup $COMMAND > logfile
    
  • use env to set the variable

    COMMAND="env FORMAVEN=valueForMaven mvn clean install -P $MAVEN_PROFILE"
    
Share:
19,822

Related videos on Youtube

Vincent
Author by

Vincent

Updated on September 18, 2022

Comments

  • Vincent
    Vincent over 1 year

    (I'm editing an existing Bash script, so I'm probably making a silly mistake here...)

    I have a shell script that saves a command with an environment variable as its argument like this:

    COMMAND="mvn clean install -P $MAVEN_PROFILE"
    

    It then executes the command with nohup roughly as follows:

    nohup $COMMAND > logfile
    

    This works.

    Now, I want to set an environment variable that can be accessed in Maven. I've tried several things like the following:

    COMMAND="FORMAVEN=valueForMaven mvn clean install -P $MAVEN_PROFILE"
    

    ...but then it just terminates with:

    nohup: failed to run command `FORMAVEN=valueForMaven': No such file or directory
    

    I feel like there are several unrelated concepts at work here, none of which I understand or even know about. What do I need to be able to do the above?

  • Vincent
    Vincent over 7 years
    Excellent! I knew it had to be easier than the directions I was thinking of...
  • cristianoms
    cristianoms almost 4 years
    How come I never realised that it was as simple as export THEN run the command??? Thanks!