nohup can't save the pid of the process into file with xargs command
By John Campbell •
In the script that I am building I am having problem saving the pid to file.
If run in the terminal the command below:
nohup mvn spring-boot:run > $HOME/logs/app.log 2>&1 & echo $! > $HOME/pids/app.pidI am unable to this with xargs command like below:
echo "dirA" | xargs -L 1 -I {} sh -c "cd '{}'; echo 'Changed directory to' '{}';
nohup mvn spring-boot:run > $HOME/logs/app.log 2>&1 & echo $! > $HOME/pids/app.pid; "The app.logs file are generated successfully and have the correct content. Meanwhile the app.pid is generated with empty content.
I will appreciate any help in figuring out this problem.
Regards, Rando.
1 Answer
The double quotes around your sh -c "..." command allow $! to be expanded by the interactive shell.
Try
echo "dirA" | xargs -L 1 -I {} sh -c ' cd "$1"; echo "Changed directory to $1"; nohup mvn spring-boot:run > $HOME/logs/app.log 2>&1 & echo $! > $HOME/pids/app.pid;
' sh {}There's no need to quote {} in most shells.