M HYPE SPLASH
// general

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.pid

I 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.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy