문제

I have a bash script to run a command via nohup, I need the proccess id in the script.

I tried this:

#!/bin/bash
nohupResult=$((nohup mycommand > nohup.out &) 2>&1)
echo $nohupResult
exit 0

But the $nohupResult is null.

I tried also this:

nohupResult=`nohup mycommand > nohup.out &`

But the $nohupResult is null either.

If I run the command nohup mycommand > nohup.out & in shell, I will get some outputs like:

[1] 447019

But how could I get the process id in the script?

Any help would be much appreciated.

도움이 되었습니까?

해결책

The pid of the last background job is in $!

nohup mycommand &
pid=$!

다른 팁

You could just use:

echo $!

This should return the PID of the last started background process.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top