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